I am trying to connect my app, developed in C# with a SQL Server database.
The program is done! It's a mobile app.
My database is on C:\ProgramFiles\MyAppName\MyDatabase.sdf
My code line is:
SqlConnection conn = new SqlConnection("server =" + iP.Text +"," + port.Text + ";integrated security=false;Initial Catalog=" + DB.Text + ";User ID=" + userName.Text + ";Password=" + Pass.Text + ";Trusted_Connection=False;");
iP.Text = my IP (102.168.XXX.XXX)
port.Text = 49214 or 1433
DB.Text = "MyDatabase.sdf"
userName.Text= "sa"
pass.Text= "MyPass"
But when I try to connect it, the app says:
er.Message = "El servidor SQL Server no existe o se ha denegado el acceso.
My server name is the same that my userName?
The application was made by someone else, I did nothing. But now I have to change some things and make it better. There is no manual
Any idea? I really don't know what to do
You are using Sql Server Compact Edition (SDF file) not Sql Server.
The classes needed to connect to this kind of database are different
SqlCeConnection conn = new SqlCeConnection(......)
The classes for Sql Server cannot parse correctly your connection string and you get the mentioned error. Of course, the classes for Sql Compact Edition require a reference to the appropriate DLL
System.Data.SqlServerCe.dll
and the appropriate using statement at the beginning of your code file
using System.Data.SqlServerCe;
As last note, the connection string for a SQL CE database is simply
"Data Source = MyDatabase.sdf; Password ='<pwd>'"
doesn't seem possible to pass a specific user. See connectionstrings.com
Related
I am using sql server 2008 R2 and sql server managment studio .
I want to attach the local database file at run time .
I provided the local database path via texbox to the connection string .
But it gives me error
invalid object name .
con = new SqlConnection("Data Source=
(LocalDB)\\MSSQLLocalDB;AttachDbFilename=" + databasepathtextbox.Text + ";
Integrated Security=True; MultipleActiveResultSets =True;");
and command to insert data:
con.Open();
cmd=new SqlCommand(" insert into Firsttimecheck (checkfilepath)
Values('" + textBox1.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();
And the path which i get via texbox as
D:\\data\\datainfo\\schoinfo\\Database1.mdf
Please help, how to add local database file path at run time?
Finnaly I figure out the error . The error was \\ I replace it with \ and it worked
I can't connect via ASP.NET to a named instance of a sql server, that itsn't the default instance.
My servername is NAMEOFSERVER\NAMEOFINSTANCE.
If I try to connect to the default instance of the server, it works.
I use the following connection string:
connection_string = "Data Source=" + servername + ";Initial Catalog=" + db_catalog + ";User Id=" + user + ";Password=" + password + ";persist security info=False;Trusted_Connection=No;Connection Timeout=1000"
The SQL Server Browser service is running.
Make sure your connection string have the pattern above:
var connectionString = $"Data Source=myServerName\\myInstanceName;Database=myDataBase;User Id=myUsername; Password=myPassword;",
Also, make sure you can connect via SQL Management Studio, since instances of the SQL Server can limit access to some users (make sure you connect to the same user name + password you want your app to use).
EDIT
If the user you are using to connect is not part of a Windows account, please open your instances security configuration and make sure Mixed Mode is enabled.
https://learn.microsoft.com/en-us/sql/database-engine/configure-windows/change-server-authentication-mode
https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql/authentication-in-sql-server
You might want to check if your variable "servername" contains a double backslash. This is needed because backslash is a escape character.
connection_string = "Data Source=" + servername.Replace("\\", "\\\\") + ";Initial Catalog=" + db_catalog + ";User Id=" + user + ";Password=" + password + ";persist security info=False;Trusted_Connection=No;Connection Timeout=1000"
I have been looking around on the internet to find the proper connection string format, but I find different ones all over the place from different topics posted years between. I can't seem to find the right one. I am using a MySQL Database installed as a Windows Service.
Printed here is my Connection String (I removed the Port after this picture was taken and the last semicolon is there but I omitted it accidentally). But I get the following error message:
I get Error 40 from this. Anyone know the actual correct format? I feel stupid for asking haha
EDIT:
As Requested, instantiation Code for the Connection:
StringBuilder sb = new StringBuilder();
sb.Append("Data Source=MySQL;");
sb.Append("Server=" + Server + ";");
sb.Append("Database=" + Database + ";");
sb.Append("UID=" + UserID + ";");
sb.Append("PWD=" + Password + ";");
Console.WriteLine(sb.ToString());
MySqlConnection conn = new MySqlConnection(sb.ToString());
conn.Open();
return conn;
It is searching for an SQL Server, whereas you want it to connect it to a MySQL database. I guess you are using SQLConnection to connect to the database. That will not work.
You need to either use OleDBConnection or MySQLConnection object to connect to a MySQL database.
In my application, i make a dynamic connection string:
server = "Server = .\\" + this.comboBoxListInstances.SelectedItem.ToString() + ";";
connectionString = server + attachDatabase + databaseName + "Integrated Security = true";
On my laptop (using SQL server 2008 express), the result is:
// server = "Server = .\\SQLEXPRESS;" (I select SQLEXRESS in comboBox)
// attachDatabase = "AttachDbFileName = |DataDirectory|\\Resources\\DT.mdf;"
// databaseName = "Database = DATA;";
// so the conectionString is : "Server = .\\SQLEXPRESS;AttachDbFileName = |DataDirectory|\\Resources\\DT.mdf;Database = DATA;Integrated Security = true"
I read registry to find all SQL server Instances and let the user choose which they want.
This conectionString work fine on my laptop and my friend who use SQL server express too. However, when i run my app on another friend's laptop with SQL server 2008 R2 installed, it throws an exception:
It say the connection string is invalid, It is :
"Server = .\\MSSQLSERVER;AttachDbFileName = |DataDirectory|\\Resources\\DT.mdf;Database = DATA;Integrated Security = true"
when I try to temporarily disable comboBoxListInstance and use this conectionString
"Server = (local);AttachDbFileName = |DataDirectory|\\Resources\\DT.mdf;Database = DATA;Integrated Security = true"
The app works! So, I think there is a difference between the conection string for SQL server express and SQL server. Is it right? There is a question similar to mine here, and they say that there is no difference. If they are right, what is the problem of my connectionString?
P/S: sorry for my bad grammar
It looks like you just proved that SqlExpress installs its default named instance as "MSSQLSERVER" where as full sql installs an unnamed default instance of blank.
(local) means " .\ " " 127.0.0.1" WHATEVER instance is at 1433.
.MSSQLEXPRESS means explicitly .\MSSQLEXPRESS which may or may not be the (local) default instance.
As An Aside usually the most relevant difference at this point is that regular SQL will install with the ports open and TCP clients ready to go.
SQL Express will only allow local "dev" type of connections until you activate the external ports and client protocols.
So I have a client program and a database on sql server that's on my pc (localdb). I'm trying to set connection on, but it seems like provider is wrong. My connection string looks like this:
connection.ConnectionString =
"Data Source=(localdb)\\Projects;" +
"Provider=MSIDXS;" +
"Initial Catalog=TermPaperWork;" +
"User id=DAZZBOURGH\\Dazzbourgh;" +
"Integrated Security=True;" +
"Password=;";
So everything is OK there, except provider. I don't get what it is and how to use it and how to know which provider suits my database.
How do I find out my provider for exact base?
I always use http://www.connectionstrings.com when I have questions on the format of a connection string.