这样写不对吗? string strCon ="Data Source=(local);DataBase=kaoshi;User id=sa;pwd=123"; SqlConnection con = new SqlConnection(strCon); con.Open(); int count1=0; int count2=0; SqlCommand mycom1= new SqlCommand("select * from chengji where StudentID="+ Application["StudentID"]+"and LessonID=01",con); SqlCommand mycom2= new SqlCommand("select * from chengji where StudentID="+ Application["StudentID"]+"and LessonID=02",con); count1= Convert.ToInt32(mycom1.ExecuteScalar()); count2= Convert.ToInt32(mycom2.ExecuteScalar()); Label1.Text=count1.ToString(); Label2.Text=count2.ToString();
count1= mycom1.ExecuteNonQuery();试试这样取
ExecuteScalar()这是返回结果集首行首列的值,跟你问的影响的行数不搭边!
using (SqlConnection sqlcon = new SqlConnection(strcon)) { sqlcon.Open(); using (SqlCommand comm = new SqlCommand(sql, sqlcon)) { comm.Parameters.Clear(); if (sqlparam != null) { comm.Parameters.AddRange(sqlparam); } return comm.ExecuteNonQuery(); } }comm.ExecuteNonQuery();就是返回影响行数 存储过程里面可以用@@ROWCOUNT获得受影响的行数。
SqlConnection con = new SqlConnection(strCon);
con.Open();
int count1=0;
int count2=0;
SqlCommand mycom1= new SqlCommand("select * from chengji where StudentID="+ Application["StudentID"]+"and LessonID=01",con);
SqlCommand mycom2= new SqlCommand("select * from chengji where StudentID="+ Application["StudentID"]+"and LessonID=02",con);
count1= Convert.ToInt32(mycom1.ExecuteScalar());
count2= Convert.ToInt32(mycom2.ExecuteScalar());
Label1.Text=count1.ToString();
Label2.Text=count2.ToString();
{
sqlcon.Open();
using (SqlCommand comm = new SqlCommand(sql, sqlcon))
{
comm.Parameters.Clear();
if (sqlparam != null)
{
comm.Parameters.AddRange(sqlparam);
}
return comm.ExecuteNonQuery();
}
}comm.ExecuteNonQuery();就是返回影响行数
存储过程里面可以用@@ROWCOUNT获得受影响的行数。
ExecuteScalar是得出查询结果的第一行第一列的内容
ExecuteScalar是用来读一列【数据的读】
ExecuteNonQuery是用来返回受影响的行数【数据的增、删、改】