Hi i have a problem, i make a form with one button and i want to make in c# (windows Aplication) the follows: when i click in button i need to connect in my SQL Server. How can i do that??:?:

10 Years
Discussion Span
Last Post by kumar169

Here is a snip of code from my Login screen. It has the usual Server Name, User name and Password texboxes, and a Login and Cancel button. Mine is hardcoded for a database named SAFEnet. This is using the SQLclient (SQL2005). If you are using another type DBMS, let me know, and I will show you that code.

I use a string to contain the ConnectionString value, then use the replace method to build in the parameters. The Login button calls this code:
There are other ways of handling the string, this just makes it more readable. The bottom line is that you contruct the connection string and pass it to the open procedure of the Connection object.

Note: you will want to wrap the connection in a try..catch


string ConStr = "Data Source=<SERVER>;Initial Catalog=SAFENET;USER ID=<LOGIN>;Password=<PASSWORD>";
ConStr = ConStr.Replace("<SERVER>", cbServer.Text);
ConStr = ConStr.Replace("<LOGIN>", edOperator.Text);
ConStr = ConStr.Replace("<PASSWORD>", edPassword.Text);
btnLogin.Enabled = false; // Make the Button Disabled;
btnCancelQuit.Enabled = false;
// Check these credentials
SqlConnection conn = new SqlConnection( ConStr );
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.