



连接数据库后执行查询,并将查询结果反馈到变量显示出来private void button1_Click(object sender, EventArgs e)
{
try {
string conString = "server=host;database=test;uid=test;pwd=test123";
SqlConnection conn = new SqlConnection(conString);
conn.Open();
MessageBox.Show("连接数据库成功", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("报错,原因:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string openSql = "Server=211.149.211.211;DataBase=test;Uid=test;Pwd=123456";
SqlConnection con = new SqlConnection(openSql);//数据库连接对象
SqlCommand cmd = con.CreateCommand();//执行操作对象
cmd.CommandText = "select c_memo from countlist";
con.Open();
string name = cmd.ExecuteScalar().ToString();
MessageBox.Show("连接数据库成功,查询内容为:" + name, "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
con.Close();
}
catch (Exception ex)
{
MessageBox.Show("报错,原因:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
本文出自技术小学生博客,https://blog.tag.gg/showinfo-23-35768-0.html 转载请注明出处,否则追究法律责任。



文章评论 本文章有个评论