I just started creating a local database in Visual Studio for the first time and I'm having a hard time making it work.
In modify connection I tested and it works and also got the connection string:
string ConnectionString = #"Data Source=D:\Bratulescu Mihai\WindowsFormsApplication1\WindowsFormsApplication1\NodeBDatabase.sdf";
sqlConn = new SqlConnection(ConnectionString);
sqlConn.Open();
But when it tries Open() it cannot connect. I don't know much about this whole server thing, I just created the db and it's DataSet.
If you are using SQLCe since I see you are reffering to an sdf file you should add a reference of System.Data.SqlServerCe and try:
using System.Data.SqlServerCe;
....
sqlConn = new SqlCeConnection(ConnectionString);
sqlConn.Open();
are you sure about the connection string?
You could take a look at this site: http://www.connectionstrings.com/sql-server-compact/
Related
I am trying to connect to SQL Server and get data. This is what I did, but it's not working:
string connectionString;
SqlConnection cnn;
connectionString = #"Data Source=(IP)\PC-NAME\SQLEXPRESS,3306;Network Library=DBMSSOCN;Initial Catalog=dbase;User ID=sa;Password=password";
cnn = new SqlConnection(connectionString);
cnn.Open();
MessageBox.Show("Connection Open !");
cnn.Close();
Your Code is Correct, except your connection string i think
So, first, connect to your database via server Explorer in VisualStudio\View menu
Then right-click on your database and select properties and check the connection string and copy that for test
I think you have a problem with your connection string.
Check your connection string using this given example:
Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial
Catalog=myDataBase;User ID=myUsername;Password=myPassword;
When use Oledb c# connection
I noticed that a lot connection using file.
But how to connect to localhost using oledb?
I created database and tables using Microsoft SQL Server Management that connect with SQL Express and using window authentication
When using this function i don't know how should convert to connect to localhost
//Want the connString to connect localhost instead of file
public static string connString = #"Provider=Microsoft.JET.OLEDB.4.0;data source=" + Path + "\\database\\errDB.mdb";
public static OleDbConnection connection;
public myFunction()
{
string sqlString = "SELECT name,contact,accessLevel,Crudential_ID FROM errors where Crudential_ID =#ID";
connection = new OleDbConnection(connString);
OleDbCommand command = new OleDbCommand(sqlString, connection);
//Open connection
connection.Open();
command.Parameters.Add("#ID", OleDbType.VarChar);
command.Parameters["#ID"].Value = "test";
//Read from database
OleDbDataReader reader = command.ExecuteReader();
if(reader.HasRows)
{
.....
}
connection.Close();
}
connectionstrings.com - true to its name - is indispensable when you frequently need to construct connection strings. For your specific case, this would be the relevant section.
Based on that, your connection string should look something like this:
Provider=SQLNCLI11;Server=.\SQLEXPRESS;Database=SOMEDATABASE;Trusted_Connection=yes;
To break it down:
SQLNCLI11 is the SQL Native Client OLEDB provider. You can see available providers in SQL Management Studio, under Server Objects > Linked Servers > Providers.
.\SQLEXPRESS is your servername and instance. The . is shorthand for localhost (you can also use localhost if you prefer), and SQLEXPRESS is the default instance name that SQL Express installs under.
SOMEDATABASE - whatever your database name is.
Trusted_Connection=yes - Use windows authentication. Sometime you see it as Integrated Security=SSPI. They are one and the same.
If you are using SQL Express then I would suggest using a System.Data.SqlClient.SqlConnection object to make your connection. You will only need your server name to connect.
Server=ServerName\SQLEXPRESS;Database=Blah;User ID=user;Password=pw
I have a .mdf database called Persons in my project in Visual Studio 2012. And have a Windows forms application.
I have a button on the form called addToDb. When clicking it, I want to add something to the database.
This is my code:
SqlConnection myDbConnection = new SqlConnection ()
myDbConnection.SqlString = "Addr=Persons.mdf;"
I don't know the connection string and I find it in the net but it makes for me an error and doesn't open the database connection when I use:
myDbConnection.open();
error 40 - Could not open a connection to SQL Server
You should refer to www.connectionstrings.com if you do not know the correct connectionstring.
If you use SqlExpress, which I conclude from you .mdf file the syntax would be
string connectionString =#"Data Source=.\SQLEXPRESS; AttachDbFilename=c:\path\tofile.mdf; Integrated Security=True;Connect Timeout=30;User Instance=True";
using(SqlConnection myDbconnection = new SqlConnection(connectionString)
{
myDbConnection.Open();
//DoStuff
}
Hello I'm trying to run a simple sql command on a DB from MS VS C# 2010 and I have encountered a error I have never seen before the relevant code is:
SqlConnection comCon = new SqlConnection(#"Data Source=C:\\Users\\George\\Desktop\\programming\\C#workspace\\Projects\\Examen\\Examen\\Companie.mdf;Initial Catalog=Proiect;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "UPDATE Proiect SET Buget = Buget + 500 WHERE (Buget > 0)";
cmd.Connection = comCon;
comCon.Open();
Console.WriteLine(cmd.ExecuteNonQuery().ToString());
comCon.Close();
And the error is Keyword not supported: 'data source'
The main problem is that I'm not used to creating these sqlconnections by hand so please tell me if I'm missing something.
You are using the wrong structure. To attach a database file, you need to use the following structure:
SqlConnection sqlConnection =
"Server=DatabaseServerName;AttachDbFilename=d:\Database\Database.mdf;
Database=DatabaseName; Trusted_Connection=Yes";
You need to have the right permissions on both the target file and database server to attach the databse and establish the connection.
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;
If it's not an ASP.NET application don't use the DataDirectory syntax and just use the full c:... path.
SQLiteConnection conn = new SQLiteConnection("Data Source=/data/bakkal.db3;");
conn.Open();
conn.Close();
I am new at programming so maybe the problem is a very dumb one, sorry for that.
I am trying to connect my project with a database which exists in the directory listed above. But the project gives an error at "conn.Open();" line which is just "unable to connect database". Database has no passwords or etc, it is just a very small database with 2 columns.
I don't think it is going to change anything but my project is a WPF application project, maybe differs.
Thanks for any help
If the database file is located in the same folder as the executable you could try this:
using (var conn = new SQLiteConnection(#"Data Source=|DataDirectory|bakkal.db3"))
{
conn.Open();
}
If it is in a subfolder:
#"|DataDirectory|data\bakkal.db3"
If not use absolute path:
#"c:\somepath\data\bakkal.db3"
Write out the location of the database completely [drive][path][databasefile]
using (SQLiteConnection connection = new SQLiteConnection(#"Data Source=c:\data\bakkal.db3"))
{
connection .Open();
}