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())
{
//...
}
}
Related
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
I try to connect to a MySQL database on a server a friend of mine has created, but it still says it cannot connect to the server!
Here is my code:
String connectionString = "Persist Security Info=False;database=qwertyui;server=www.qwertyuiop.com;Uid=asdfghj;Pwd=qazwsxedc;Connect Timeout=30";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("INSERT INTO etc.... ");
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
cmd.Parameters.AddWithValue( etc.... );
connection.Open();
int r = cmd.ExecuteNonQuery();
}
It failes on the connection.Open() instruction throwing an exception -> cannot connect to the server.
Any ideas please?
SqlConnection is a SQL Server client.
You need to download a MySql client from NuGet.
First download the MySQL Connector.
Make sure you add a reference to the DLL (+ set copy local to true !)
Add using MySql.Data.MySqlClient; on top of your class.
As for the Connection String, do not add http or www in front of server. As it will try to connect to apache (port 80) instead to MySQL. The default port of MySQL is 3306.
string connectionString = "Persist Security Info=False;database=qwertyui;server=qwertyuiop.com;Uid=asdfghj;Pwd=qazwsxedc;port=3306";
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
string query = "INSERT INTO table VALUES (#parameter)";
connection.Open();
using (MySqlCommand cmd = new MySqlCommand(query, connection))
{
cmd.Parameters.AddWithValue("#parameter", parameter);
cmd.ExecuteNonQuery();
}
}
If this is not working. Make sure MySQL allows remote access, if this is not the case, you can keep trying forever without any result.
First thing that comes to mind is that you will have to use a MysqlConnection and command.
http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqlconnection.html
Is it a local database?
And if with all of above does not work try to get grant to all privileges(GRANT Syntax)!
Basically it will go something like:
GRANT ALL PRIVILEGES ON DBname.* to 'username'#'IPadress' IDENTIFIED BY 'password';
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.
I have created a SQL Server Compact Database (.sdf file) and I want to be connected to it for do some insert , delete ... .
This is my creation code for it:
if (File.Exists(dbfilename))
File.Delete(dbfilename);
string connectionString = "Data Source=" + dbfilename + """;
SqlCeEngine engine = new SqlCeEngine(connectionString);
engine.CreateDatabase();
engine.Dispose();
SqlCeConnection conn = null;
try
{
conn = new SqlCeConnection(connectionString);
conn.Open();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "CREATE TABLE Contacts (ID uniqueidentifire, Address ntext)";
cmd.ExecuteNonQuery();
}
catch { }
finally
{
conn.Close();
}
Is it true?
How can I connect to it?
That connection string is broken.
"
is not a valid entity where you are trying to use it. Fix your connection string.
Also, you will need to associate the command with the connection, either in the constructor or after the fact.
Please read through an example such as this one, which was the first google result for "connect sql compact example 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