110,534
社区成员
发帖
与我相关
我的任务
分享
string strCount = "Select Count(*) From 学生信息 Where 性别='男'";
SqlCommand countCmd = new SqlCommand(strCount, conn);
用指定的sql语句和数据库连接,创建sql命令对象,执行对应的sql语句。
{
SqlConnection conn = new SqlConnection("data source=(local); initial catalog=JWInfo; integrated security=SSPI");
string strCount = "Select Count(*) From 学生信息 Where 性别='男'";
SqlCommand countCmd = new SqlCommand(strCount, conn);
if ((conn.State == ConnectionState.Closed)) {
conn.Open();
}
count = countCmd.ExecuteScalar();
}
string connString = "data source=.;integrated security=SSPI;database=test";
using (SqlConnection conn = new SqlConnection(connString))
{
try
{
conn.Open();
string sqlstr = "select count(*) from jiaban";
SqlCommand cmd = new SqlCommand(cmdtext, conn);
textBox1.Text = cmd.ExecuteScalar().ToString();
}
catch (Exception ex)
{
MessageBox.Show(this,ex.Message,"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally
{
conn.Close();
}
}