Connecting to remote SQL Server in C#.Net - c#

I'm trying to connect to the database, and it keeps telling me "invalid instance".
Here's my code:
string connectionString = "Driver={SQL Server};Server=server;Database=db;;Uid=user;Pwd=pass;";
OdbcConnection MyConnection = new OdbcConnection();
MyConnection.ConnectionString = connectionString;
MyConnection.Open();
What is the problem?
Thanks!

Try using this.
Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
And if you are using MS Sql server, better use with SqlConnection.
using(SqlConnection conn = new SqlConnection(connestionString)){
conn.open();
..
}
http://www.connectionstrings.com/sql-server-2005

Related

MS SQL ODBC Connection with NO Windows Authentication

I'm trying to connect to MS SQL using ODBC but keep getting the
"Login failed for user 'User-PC\User'" error.
web.config
<add name="SQLDbConnection" connectionString="Server=127.0.0.1; Database=HMS; Integrated Security=false; User Id=sa; password=root" providerName="System.Data.Odbc "/>
C#
string query = "...";
OdbcConnection msSQLConnection = new OdbcConnection(strConnection);
OdbcCommand command = new OdbcCommand(query, msSQLConnection);
command.Connection.Open();
I tried using the below and it's ok. Any idea how I can get ODBC to work?
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLDbConnection"].ToString()))
{
SqlCommand cmd = new SqlCommand("SELECT COMPANY_ID from COMPANY", cn);
cn.Open();
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
rdr.Read();
}
SQL Connection and ODBC Connection do not use the same connection strings. For the ODBC connection you need to specify the driver.
For SQL Server 2012:
Driver={SQL Server Native Client 11.0};Server=127.0.0.1;Database=HMS;Uid=sa;Pwd=root;
You should use something like this :
DRIVER={MySQL ODBC 3.51 Driver}; SERVER=127.0.0.1; DATABASE=HMS; USER=sa; PASSWORD=root;
Then take a look in here :OBDC example
and then in here : Connection strings

Unable to connect to a Local DB (mdf) from C# form application

I tried to play with Database creation and queries. In order to do that I started a C# form application, added a Database Service then added a Table with some values, then I wanted to use some code to retrieve those values.
Here's the code:
string conn = "data source = ./SQLEXPRESS; AttachDbFilename=C:\\Users\\Asus\\Desktop\\RobaMia\\SQLSERVER\\WindowsFormsApplication3\\WindowsFormsApplication3\\Database1.mdf; Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection sql = new SqlConnection(conn);
sql.Open();
MessageBox.Show("Connection Opened");
sql.Close();
Sadly the program throws an exception when comes to the open because it seems it cannot find the Database...
"Server not found or not accessible"
I don't know what is the problem, what would you suggest?
Ok, it seems to work now but I get an incorrect syntax for my query
string conn = "Server=(LocalDB)\v11.0; AttachDbFilename=C:\Users\Asus\Desktop\RobaMia\SQLSERVER\WindowsFormsApplication3\WindowsFormsApplication3\Database1.mdf; Integrated Security=True;Connect Timeout=30;User Instance=False";
string queryString = "SELECT * FROM Table";
SqlConnection sql = new SqlConnection(conn);
sql.Open();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand command = new SqlCommand(queryString, sql);
/* --->here I get the error*/ command.ExecuteNonQuery();
DataSet data = new DataSet();
adapter.Fill(data);
MessageBox.Show(data.ToString());
sql.Close();
It looks like the data source part your connection string is wrong. It should be:
"Data source=.\\SQLEXpress"
Complete:
string conn = "Data source=.\\SQLEXpress; AttachDbFilename=C:\\Users\\Asus\\Desktop\\RobaMia\\SQLSERVER\\WindowsFormsApplication3\\WindowsFormsApplication3\\Database1.mdf; Integrated Security=True;Connect Timeout=30;User Instance=True";
https://www.connectionstrings.com/sql-server/
As an additional note, you may be best off placing this in an app.config or web.config file just encase you reference the connection string multiple times and later you decide to change the value of it.
Have you tried(LocalDB), instead of SQLExpress?
"Server=(localdb)\\Test;Integrated Security=true;AttachDbFileName= myDbFile;"
http://www.asp.net/mvc/overview/getting-started/introduction/creating-a-connection-string

Keyword not supported in Connection String: 'database'

Very new to C# and VS2012.
I'm trying to connect to a local database connection.
Here is the code
string selectSql = "select * from Tasks";
string connectionString = "Data Source=adamssqlserver;database=master;Integrated Security=true;";
using (var cn = new SqlCeConnection(connectionString))
using (var cmd = new SqlCeCommand(selectSql, cn))
{
cn.Open();
using (var reader = cmd.ExecuteReader())
{
//do something
}
}
Here is the error
Keyword not supported: 'database'.
If I put in Initial Catalog first
"Data Source=adamssqlserver;Initial Catalog=etc;"
Then the error gives the same message but for "Initial Catalog".
Here is my data connection
You are using SqlCeConnection not a SqlConnection
This class (SqlCeConnection) is for Sql Compact Edition where the syntax rules of the connection string are different. For example:
Data Source=MyData.sdf;Persist Security Info=False;
Instead your connection string is for a Sql Server or Sql Server Express.
So, if your target database is a SqlServer db as your tag indicates then you need to use
using (var cn = new SqlConnection(connectionString))
using (var cmd = new SqlCommand(selectSql, cn))
{
....
}
Instead of Data Source, try Server, e.g:
string connectionString = "Server=adamssqlserver;Database=master";
This website contains good information for setting up connection string. There are so many options I usually have to turn to a reference to get it set up correctly.
Just a reminder: when using MS Access databases, you need to use OleDbConnection and OleDbCommand, not SqlConnection and SqlCommand. 'Provider' in the connection string for SqlConnection is invalid AFAIK.
Are you missing the " from the connection string section.
Should be
<add name="StevenTestEntities"
connectionString="metadata=res://*/Model.TestModel.csdl|res://*/Model.TestModel.ssdl|res://*/Model.TestModel.msl;
provider=System.Data.SqlClient;
provider connection string="Data Source=Data Source=D000097;
Initial Catalog=StevenTest;
Integrated Security=True;MultipleActiveResultSets=True&qout;"
providerName="System.Data.EntityClient" />
Try that out.

ConnectionString for SQL Server

I have xampp installed in my computer. I am trying to access data with ADO.Net. The connection string I am using is given below:
string connectionString = "Server = localhost; Database = magento; User Id = magento; Password = abcd;";
SqlConnection con = new SqlConnection(connectionString);
string cmdString = "SELECT date_added,title,description,url FROM adminnotification_inbox";
SqlDataAdapter da = new SqlDataAdapter(cmdString, con);
ds = new DataSet();
da.Fill(ds,"prog");
dt = ds.Tables["prog"];
currRec = -1;
totalRec = dt.Rows.Count;
button3.Enabled = true;
I am able to log in with the above user id and password in phpmyadmin, but cannot access the database with the above connection string. please help. Thanks in advance.
MySQL has its own ADO.NET connector: http://dev.mysql.com/downloads/connector/net/6.6.html#downloads
If you use that, you can create a MySqlConnection: http://dev.mysql.com/doc/refman/5.5/en/connector-net-tutorials-intro.html
The basic SqlConnection is used for Microsoft's own SQL Server products.
9-22-14 - Hope others see this if you don't:
You need a driver in your connect string I believe. "MySQL ODBC 3.51 Driver" is the Window's driver name.
string connectionString ="Driver={MySQL ODBC 3.51 Driver}; SERVER= .... ok put the rest of your connect string here.
Note: this is the string to connect to a MySQL db using MS Access VBA:
Dan

Establishing connection with SQL Server 2008

This is a very elementary I realize, I have recently started working with asp.net and establishing a connection with an Access database was simple enough...
string insertedBookTitle;
conn = new OleDbConnection(#"Provider=Microsoft.Jet.OleDb.4.0;
Data Source=" + Server.MapPath("App_Data\\BookRateInitial.mdb"));
conn.Open()
What is the code I use to connect to SQL Server 2008, with the same name (BookRateInitial)?
Kind regards
Here's a sample connection string for standard security (username and pwd):
Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
You need to do a:
using(SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
//Use connection here
using(SqlCommand cmd = conn.CreateCommand())
{
//...
}
}

Categories

Resources