vs 连接sql,怎么也连不上,翻遍了网站,花了3个多小时才从网上找到原因 ,原来SqlConnection("Data Source=172.23.6x.xx;Initial Catalog=elementinfo;Persist Security Info=True;User ID=xx;Password=**");的格式不规范,搞成了SqlConnection conn = new SqlConnection("server=172.23.64.XX,uid=XX,Pwd=**,database=elementinfo");网上竟然很难找到准确的解决方案,害得我硬是盯着屏幕浪费了n分钟,n大于180啊!~~
不过还好,终于在1点半找到了答案。写来与菜鸟们共勉。
using System.Data.SqlClient;
SqlConnection conn = new SqlConnection("Data Source=172.23.6x.xx;Initial Catalog=elementinfo;Persist Security Info=True;User ID=xx;Password=**");
String observertimes = "2009101423";
String TableName = "tabCity";
String term = " where observetimes='" + observertimes + "'";
String sqlStr = "select * from " + TableName;
SqlCommand catCMD = conn.CreateCommand();
catCMD.CommandText = sqlStr;
conn.Open();
SqlDataReader myReader = catCMD.ExecuteReader();
string aa = "";
while (myReader.Read())
{
string ss = String.Format("{0}, {1}", myReader[0], myReader[1]);
aa += ss;
}
myReader.Close();
conn.Close();
textBox1.Text = aa;