I am creating a small project by using winforms in C# using Visual Studio 2010 and SQL Server 2008 - When I am creating setup and running it on the same computer it is working very well - but when I am running it on a different computer I got an error (Invalid object "tbl_name")! I'm using (SQL Server authentication) and my connection code is:
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = ".";
builder.InitialCatalog = "Sectors";
builder.IntegratedSecurity = false;
builder.Password = "123";
builder.UserID = "MAK";
builder.AsynchronousProcessing = true;
builder["Trusted_Connection"] = true;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = builder.ConnectionString;
conn.Open();
Please help me find the cause of the error and resolve it.
You could remove the statement "builder["Trusted_Connection"] = true;" and also try to change the connection settings (like DataSource to localhost).
Related
Want to restore a database backup from linked server A to a database located on linked-server B using C#. prefer to use SMO.
I can restore from my local backup to local machine.
{
conn = new ServerConnection
{
ConnectionString = #"Data Source =
(localdb)\MSSQLLocalDb;Initial Catalog=master;Integrated Security=true",
};
try
{
//Restore Full
srv = new Server(conn);
//lsrv = srv.LinkedServers[#"DEVSQL\ALPHA"]; need to figure out how to restore to linked server instead of local.
//srv.KillAllProcesses("G4TestNew");
var res = new Restore();
res.Database = "G4TestNew";
res.Action = RestoreActionType.Database;
filePath = #"\\CABCSERVER\Database\Temp\Full.bak";
res.Devices.AddDevice(filePath, DeviceType.File);
res.ReplaceDatabase = true;
res.NoRecovery = true;
var dataFile = new RelocateFile("G4Test", #"C:\TBD\G4Test.mdf");
var logFile = new RelocateFile("G4Test_log", #"C:\TBD\G4TestNew.ldf");
res.RelocateFiles.Add(dataFile);
res.RelocateFiles.Add(logFile);
res.SqlRestore(srv);
}
EDIT(Adding more detail):.
In this case the linked servers are accessed via 'sql server authentication' and application does not have access to the credential required to connect directly and can use 'Integrated Security' to connect to localdb only.
In SMO you would not connect to one server and then administer a linked server. Instead connect directly to the target server. eg:
ConnectionString = #"Data Source =
DEVSQL\ALPHA;Initial Catalog=master;Integrated Security=true",
srv = new Server(conn);
I'm trying to open a connection to my university's database that runs on Oracle 11g 11.2.0.1.0 (Oracle.ManagedDaraAccess v18.3.0 is installed on visual studio) with c#.
When I attempt to connect, it throws:
Exception thrown: 'Oracle.ManagedDataAccess.Client.OracleException' in Oracle.ManagedDataAccess.dll"
during the con.open() function at me with no further explanation.
Is there any way to see where the error occurs?
OracleConnection con = new OracleConnection();
OracleConnectionStringBuilder ocsb = new OracleConnectionStringBuilder();
ocsb.Password = pass;
ocsb.UserID = user;
ocsb.DataSource = "<address>:<port>/orcl";
con.ConnectionString = ocsb.ConnectionString;
con.Open();
Add a try catch block around the open() call and resolve the ORA error that shows up in the OracleException
I am using the following code to connect to sql throgh windows authentication.
string connctionstring = "connectionString={0};Database={1};Integrated Security=SSPI;";
string _connctionstring = string.Format(connctionstring, datasource, initialCatalogue);
SqlConnection _connection = new SqlConnection(_connctionstring);
_connection.Open();
But i am getting the following error. Help please.I am able to login through sql server.
The connection string format is not correct
Change to this:
string connctionstring = "Data Source={0};Database={1};Integrated Security=SSPI;";
Or
string connctionstring = "Server={0};Database={1};Integrated Security=SSPI;";
While Peyman's answer does cover the basic issue (connectionString is not a valid key for the string) a better solution is to use a SqlConnectionStringBuilder, this will also help you do proper escaping if you have odd characters in your string (for example if your database name contained a space)
var scsb = new SqlConnectionStringBuilder();
scsb.DataSource = datasource;
scsb.InitialCatalog = initialCatalogue;
scsb.IntegratedSecurity = true;
//You also really should wrap your connections in using statements too.
using(SqlConnection connection = new SqlConnection(scsb.ConnectionString))
{
connection.Open();
//...
}
I'm connecting my windows form application to SQL server but I get this error "SqlException was unhandled" when I start the program.
Here is my code:
void ShowEmployees()
{
using (SqlConnection Connect = new SqlConnection("Data Source=(local);" + "Database='SanMarDryCleaners';" + "Integrated Security=SSPI;"))
{
string strEmployees = "SELECT * FROM employees;";
SqlCommand cmdEmployees = new SqlCommand(strEmployees, Connect);
SqlDataAdapter daEmployees = new SqlDataAdapter();
daEmployees.SelectCommand = cmdEmployees;
DataSet dsEmployees = new DataSet("EmployeesSet");
daEmployees.Fill(dsEmployees);
Connect.Open();
dgvEmployees.DataSource = dsEmployees;
dgvEmployees.DataMember = dsEmployees.Tables[0].TableName;
}
}
Here is the screenshot:
The error suggest that it can't find the SQL Server. And that makes sense because you are using a SqlConnection class which is for Microsoft SQL Server only. You have to use MySQL libary.
Download it from this site:
http://dev.mysql.com/downloads/connector/net/
The error points to a problem while connecting to the server.
A SqlConnection is for SQL-Servers. Use a library specific to MySQL and a MySqlConnection.
See:
Connector/Net Installation
MySQL Connector/Net connection strings
Not sure why this is failing...I'm sure it's my fault. Any help would be greatly appreciated.
I'm getting the classic
Cannot open database "Northwind" requested by the login.
The login failed. Login failed for user 'MyMachine\MyUserName'.
I can login just fine using windows authentication through SQL Server Management Studio.
I checked in SQL Server Management Studio to make sure that my user has permission to use the Northwind database. I also tried most of the other responses to this question posted here on stackoverflow.
This is my code:
SqlConnection dataConnection = new SqlConnection();
try
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = ".\\SQLExpress";
builder.InitialCatalog = "Northwind";
builder.IntegratedSecurity = true;
dataConnection.ConnectionString = builder.ConnectionString;
dataConnection.Open();
...
.
I'm using SQL Server 2008 Express
In your MS SQL Studio right Click the Server and go to properties Security and select SQL Server and Windows Authencation mode
then restart your server.
in your server. go to Security folder and create a new Login
enter the Username and Password. just uncheck Enforced Security (for testing purpose only)
go to User Mapping and check the database(NorthWind) you want to handle under the new Login account then db_accessadmin
Click OK
and try your code
SqlConnection dataConnection = new SqlConnection();
try
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "UNKNOWN01-PC\\SQLEXRESS2008R2";
builder.InitialCatalog = "Northwind";
//builder.IntegratedSecurity = true;
builder.UserID = "testlogin";
builder.Password = "1234";
dataConnection.ConnectionString = builder.ConnectionString;
dataConnection.Open();
}
catch (Exception)
{
throw;
}
i suspect that the real issue here is the SqlConnectionStringBuilder but i can't explain. im just a beginner. :)
I m just writing this so that I dont fall into the same trap again and again ... The default name of the database is "NORTHWND" and not "NORTHWIND"
The name is auto created by windows sql server while importing the .bak file from the oficial site.
So this is ok
static string connectionString = "data source=GMDESK028\\SQLSERVER2;initial catalog=NORTHWND;Integrated Security=SSPI;";
Try doing it this way, it's always worked for me, simpler too as you can create a new SQL connection object directly from a connection string by just specifying the string as a parameter, like this:
string connectionString = #"Server=server\instance;Database=Northwind;Integrated Security=True";
SqlConnection dataConnection = new SqlConnection(connectionString);
try
{
dataConnection.Open();
}
catch (sqlexception e)
{
Messagebox.Show("Error");
}