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??:?:

Recommended Answers

All 2 Replies

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

Regards,
Jerry

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 );
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.