2.3 Double click the button, go to the Background Code Editor, add the connection code, the specific code text is as follows:
private void Buttonconnsql_click (object sender, RoutedEventArgs e)
{
String strconnection = "server=localhost;";
Strconnection + = "initial catalog=test;";
Strconnection + = "user id=**;";
Strconnection + = "password=******;";
Strconnection + = "Connect timeout=5";
BOOL Canconnectdb = false;
using (SqlConnection objconnection = new SqlConnection (strconnection))
{
Try
{
Objconnection.open ();
Canconnectdb = true;
Objconnection.close ();
}
Catch {}
if (CANCONNECTDB) MessageBox.Show ("Database connection succeeded! "," Baidu Experience Tip: ");
else MessageBox.Show ("Database connection failed! "," Baidu Experience Tip: ");
}
}
Say the meaning of the field in the connection string:
①server: The address of the database, if the local connection, with localhost, to use IP address access, the database needs to open the remote access function, as to how to set up, please Baidu, there are many predecessors.
②initial Catalog: Database name, complete the database test created in step one
③user ID and password: username and password, in this internet age, this represents what, believe not I said, if you do not know, then you are out!
④connect timeout: The connection time-out, call the database open () method, the program enters the blocking state, the period according to our written connection string to constantly open the database, if the network is not connected or server and other field errors, there will be delay or even the risk of death, so the time-out to write a smaller point.