hello all,

I have database named 'Marketing', and want to bound DropDownList control with it using 'add connection' dialog box in the C#.net.

I gave datasource: 'Microsoft SQL Server Database File (SqlClient)'. And when I give DataBase file name as 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\Marketing_Data.MDF', I get error while testing the connection:
"An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that
under default settings SQL Server dose not allow remote connections. (Provider:SQL Network Interfaces, error: 26-Error Locating Server/Instance specified)".

So what DataBase File name I should give here for completing the database binding?

Thanks

If this database resides on the server, type its name and use your suitable authentication options and select target database from databases downlist.

What Ramy is saying is that you do not specify a File name, but rather a ConnectionString.

SqlConnection conn = new SqlConnection("Data Source=Servername;Initial Catalog=Marketing;Integrated Security=SSPI");
conn.Open();

Thanks Jerry :)

Hi Jerry, I tried this connection but I am getting an error on "conn.Open();". Could you please help me to find out what would be the reason of this error?
following is the detail error message "Compiler Error Message: CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
"

Post your code that sets up the connection string and I will take a look. The error you mention is a typical syntax issue.

// Jerry

hi can you tell me after establish the connection , how to i use this connections in all forms

You don't want to keep the connection open all the time, only when you need it. As in the matter of fact, the conn.Open(); shouldn't be in your code at all. Any sql command statement such as sqlCommand and sqlDataAdapter all can take a connection as a parameter. If you have conn declared as a global variable, then you can use that variable to pass as a connection to all the sql command statements. The statement will do the connection open and closing as necessary.

Interesting that this thread is back alive after a year.
The SqlDataAdapter will automatically take care of opening and closing the connection, but the others such as SqlCommand do not. The adpater is a nice and easy to use component, but on the down side, it is a heavy class. If you need high throughput, consider using a DataReader.

It is better to keep the connection string as a global var than an SqlConnection. It is better to create new, open, use, and dispose of an SqlConnection instance because connections can be broken when you least expect it.

However, sometimes, you do need to keep a connection open for an extended period of time when building things like mass data transports. Consider a process where 100 rows per second need to be transferred from a socket into a database. Creating 100 SqlConnections per second doesn't work (for very long :).

Most commercial applications use a Data Access Layer (DAL) class to manage their database requirements. This is usually a static class which makes it available to all forms and classes in the entire application. A DAL also decouples your application from a specific DBMS. If your company decides to move to a different or newer version of the DBMS , (if written correctly) all that needs to change is the DAL class which lowers the cost because there is minimal regression testing.

Well, off my soap box, have a Happy New Year

sqlconnection cn=new sqlconnection(user id=sqlserver username ;password=sqlserver password;database=database name);
cn.open();

send me how to check login password and username is correct or roungh by using sql server 2005 and i want code of connection string....

When you try to open the connection, SQL will throw an error if the credentials are not correct. A nice little component to use in building the connection string is SqlConnectionStringBuilder. Here is the code you are looking for using that component.

using System.Data;
            using System.Data.SqlClient;

            SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
            scsb.DataSource = "YourServerName";
            scsb.InitialCatalog = "YourDatabaseName";
            scsb.IntegratedSecurity = false; // set to true is using Windows Authentication
            scsb.UserID = "YourUserName";
            scsb.Password = "YourPassword";
            SqlConnection conn = new SqlConnection(scsb.ConnectionString);
            try
            {
                conn.Open();
                MessageBox.Show("Connection Successful", "Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (SqlException loginError)
            {
                MessageBox.Show(loginError.Message, "Failed to connect", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            finally
            {
                conn.Dispose();
            }

SqlConnection sc=new SqlConnection(" user id=sa; password=system; database=login;");
sc.Open();
loaddata();

kriss I'm Gowtham i did try this code but im getting a error on cn.open()

Man this is a really old thread...
Easiest way to do this is with the SqlConnectionStringBuilder class.
Just create an instance of that class, provide values for DataSource (aka the server), InitialCatalog (the name of the database), IntegratedSecurity (aka Windows authentication=True, or false meaning you need to supply a username and password,
and finally if not using Windows authentication, set the UserID and Password values.
Now, once you set those values, you can use its ConnectionString property for your SqlConnection instance.
That little helper class takes all the guess work out of it.
Have Fun,
Jerry

make changes in your web.config file accrdingly !

i do not about the database connectivity

hello all,

I have database named 'Marketing', and want to bound DropDownList control with it using 'add connection' dialog box in the C#.net.

I gave datasource: 'Microsoft SQL Server Database File (SqlClient)'. And when I give DataBase file name as 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\Marketing_Data.MDF', I get error while testing the connection:
"An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that
under default settings SQL Server dose not allow remote connections. (Provider:SQL Network Interfaces, error: 26-Error Locating Server/Instance specified)".

So what DataBase File name I should give here for completing the database binding?

Thanks

Hope this link will help you. This is my ask question and get solution. It can help you easy to maintain database location.
http://www.daniweb.com/forums/thread347921-2.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.