Trying to connect to SQL Server using C# - c#

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;

Related

How to access the localhost connection to MySQL database through c#?

In my C# code, I have this in my App.config
<add name="SampleDB" connectionString="Server=.;Database=Sample;Trusted_Connection=True;" providerName="System.Data.SqlClient"/>
I opened MySQL Command Client, typed in the password, then created the Sample database by typing CREATE DATABASE SAMPLE;
However, I cannot connect to the database. Do I need to specify the instance of SQL after "Server=.?
Or do I need to open the connection some other way?
Bookmark the ConnectionStrings.com. This site provides all types of connectionstring information for all types of databases and versions of drivers, including MySQL.
Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;
Pwd=myPassword;
Put a piece of code here starting from the declaration of the MySqlConnection object so we can better understand why it is not connecting because the connection string answer above is already correct. If you installed MySQL using default settings, it should be:
<appSettings>
<add key="MyConnectionSettings" value="Server=127.0.0.1;Port=3306;Database=myDataBase;Uid=root or user;
Pwd=yourpassword" />
string connectionFromConfig = ConfigurationManager.AppSettings["MyConnectionSettings"];
using(MySqlConnection con = new MySqlConnection(connectionFromConfig)){
con.Open();
string sql = "SELECT *from yourtable";
MySqlCommand cmd = new MySqlCommand(sql, con );
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr[0]+" -- "+rdr[1]);
}
rdr.Close();
}

C# Create SqlConnection instance with Connection String

I tried to create SqlConnection object with connection string such as
SqlConnection con = new SqlConnection("Connection string");
However, when the program runs, exception happens, and the exception says
the keyword, port, is not supported.
So I change connection string from
server=server name;user id= user name;password=myPassword;persistsecurityinfo=False;database=Database name;Port=port number
to
server=server_name, portNum;user id= user_name;password=myPassword;persistsecurityinfo=False;database=Database_name;
or to
server=server_name: portNum;user id= user_name;password=myPassword;persistsecurityinfo=False;database=Database_name;
But I still have problems to connect to database.
Could anyone give me how to connect database with port number?
Sincerely,
You can use the port at the end of the data source
Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;
Check http://www.connectionstrings.com/sql-server/

Error establishing connection to SQL Server

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/

How to connect to the Remote Database using Connection String in C#.net

every one. I want to connect a remote database using Sql Connection String in C#.net, I am trying it to do, but failed to connect. I am new to C#.net connections to the Database. Can any one pls tell me how to write the Connection String.
Check this website for the specific format: http://www.connectionstrings.com/
Here is a little bit of code that will connect to a dabase called myDatabase on a server called myServer, query the myTable table for the column myColumn, and insert the returned data into a list of strings.
While by no means exaustive or perfect, this snippet does show some of the core aspects of working with data in C#.
List<string> results = new List<string>();
SqlConnection conn = new SqlConnection("Data Source = myServerAddress; Initial Catalog = myDataBase; User Id = myUsername; Password = myPassword;");
using (SqlCommand command = new SqlCommand())
{
command.Connection = conn;
command.CommandType = CommandType.Text;
command.CommandText = "Select myColumn from myTable";
using (SqlDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
results.Add(dr["myColumn"].ToString());
}
}
}
There is no difference in this regard. The connection string to connect to remote server database is written same as you write to connect to local database server.
However, only Data Source changes.
Below is a sample connection string
User ID=dbUserName;Password=dbUserPwd;Initial Catalog=dbName;Data Source=remoteMachine\serverInstanceNameIfAny;
But by default sql server is not configured to Sql Server Authentication, so you need to enable
Sql server authentication
Also Create a Log in user in the database
Here are a couple of examples:
With Integrated Security
Server=RemoteMachineName\Intance; Initial Catalog=DatabaseName; Integrated Security=true;
With username and password
Server=RemoteMachineName\Intance; Initial Catalog=DatabaseName; UID=Username; PWD=Password;
You can also do that in web.config file
<configuration>
<ConnectionStrings>
<add name="YourConnectionString" connectionString="Data Source=Nameofserver;
InitialCatalog=NameofDatabase;Persist Security Info=True;
UserID=DatabaseUserID;Password=DatabasePassword" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>

Connection String Problem

I am a programmer trying to teach myself C#. I am trying to connect the the Northwind.mdf database in a form. I have used the Database Explorer to attach the database to the form, and the test connection button worked. For the connection string I am using "server=.\\sqlexpress; Trusted_Connection=yes; database=Northwind" This connection fails in SqlDataAdapter dataAdapter = new SqlDataAdapter(selectCommand, connectionString); Google has been no help. Any ideas?
Try this?
Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI;
Right click on the connection in the "Database Explorer".
Click on "Properties".
See the "Connection String" in the properties window, with its value on the right.
Is this the same connection string, as the one you posted?
Here is the connection string, I could see with a new mdf file I created
Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\kalpesh\Documents\test.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True
Assuming that the connection string is correct, you will have to escape it in c# (if it contains any of the characters that requires it. for e.g. the backslash character)
string connectionstring = #"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\kalpesh\Documents\test.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
In your case, it should be the path to the Northwind.mdf located on your machine.
Does this work?

Categories

Resources