1.
string sqlconn = "Data Source=192.168.1.81(Ip地址); Initial Catalog = Peams(数据库名称);uid=sa;pwd=123456;Connection Timeout=3(延迟);";
2. SqlConnection Sqlconn = new SqlConnection(sqlconn);
3. Sqlconn.Open();
4. string sqlStr = "select * from Area";
SqlCommand sc = new SqlCommand(sqlStr, Sqlconn);
5. SqlDataReader myRead = sc.ExecuteReader();
while (myRead.Read())
{
label1.Text += myRead.GetValue(0).ToString() + " "
+ myRead.GetValue(1).ToString() + " "
+ myRead.GetValue(2).ToString() + " "
+ myRead.GetValue(3).ToString() + " "
+ myRead.GetValue(4).ToString() + " "
+ myRead.GetValue(5).ToString() + " "
+ myRead.GetValue(6).ToString() + "\n\r";
}
2. SqlConnection Sqlconn = new SqlConnection(sqlconn);
3. Sqlconn.Open();
4. string sqlStr = "select * from Area";
SqlCommand sc = new SqlCommand(sqlStr, Sqlconn);
5. SqlDataReader myRead = sc.ExecuteReader();
while (myRead.Read())
{
label1.Text += myRead.GetValue(0).ToString() + " "
+ myRead.GetValue(1).ToString() + " "
+ myRead.GetValue(2).ToString() + " "
+ myRead.GetValue(3).ToString() + " "
+ myRead.GetValue(4).ToString() + " "
+ myRead.GetValue(5).ToString() + " "
+ myRead.GetValue(6).ToString() + "\n\r";
}
6. Sqlconn.Close();
注意事项:
1.区分是否含有\\sqlexpress;
2.不区分字母大小写;
比如string sqlconn = "DATA SOURCE=192.168.1.81; Initial CATALOG = Peams;user ID=SA;PASSWORD=123456;Connection Timeout=3;";是正确的;
3.pwd可以写作password;uid可以user id;