dataSelect你的这个使用的是
SqlConnection conn = new SqlConnection();
而这个并没有设置连接字符串所以肯定连接不上数据库
你直接这样写就行了[C#] 纯文本查看 复制代码 if (textchaxun.Text == "")
MessageBox.Show("查找的姓名不能为空!");
else
{
try
{
string chaxun = textchaxun.Text.Trim();
string sSQL = "";
sSQL = "select * from Contact where 姓名='" + chaxun + "'";
string con = "server= tongxunlu;database=(Data);integrated security=true;";
SqlConnection conn = new SqlConnection(con);
conn.Open();
SqlDataAdapter dataSelect = new SqlDataAdapter(sSQL, conn);
DataTable dt = new DataTable();
dataSelect.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
this.dataGridView1.RowHeadersVisible = true;
conn.Close();
textchaxun.Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
|