救命!帮我看看这段代码,困扰我很多天了
private void DataGrid1_UpdateCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string countStr= "select count(*) from totalCount where voteStuNum= ' "+this.Session[ "stuNum "].ToString()+ " ' ";
SqlConnection myConn=conn();
SqlCommand myCommand=new SqlCommand(countStr,myConn);
try
{
myConn.Open();
int count=(int)myCommand.ExecuteScalar();
myConn.Close();
if(count> 9)
{
Response.Write( " <script language= 'javascript '> alert( '你的票已投完! ') </script> ");
}
else
{
string myStr=DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
string myString=this.Session[ "stuNum "].ToString();
string updateStr= "update student set ticketsNum=(ticketsNum+1) where stuNum= ' "+myStr+ " ' ";
string insertStr= "insert into totalCount "+ "(voteStuNum,votedStuNum)values( ' "+myString+ " ', ' "+myStr+ " ') ";
SqlConnection myConne=conn();
SqlCommand myComm=new SqlCommand(updateStr,myConne);
SqlCommand myComman=new SqlCommand(insertStr,myConne);
try
{
myConne.Open();
myComm.ExecuteNonQuery();
myComman.ExecuteNonQuery();
myConne.Close();
}
catch
{
Response.Write( " <script language= 'javascript '> alert( '投票失败! ') </script> ");
}
}
}
catch
{
Response.Write( " <script language= 'javascript '> alert( '联系管理员! ') </script> ");
}
BindData();
}
无法更新数据库中的数据!
------解决方案--------------------1. 你这件用存储过程来做最好
2. 按你现在的处理方法,把下面代码替换为
myConne.Open();
myComm.ExecuteNonQuery();
myComman.ExecuteNonQuery();
myConne.Close();
=====================================
myConne.Open();
myComm.ExecuteNonQuery();
myConne.Close();
myConne.Open();
myComman.ExecuteNonQuery();
myConne.Close();
看能否成功
3. 或者可以这样改
string updateStr= "update student set ticketsNum=(ticketsNum+1) where stuNum= ' "+myStr+ " ' ";
string insertStr= "insert into totalCount "+ "(voteStuNum,votedStuNum)values( ' "+myString+ " ', ' "+myStr+ " ') ";
SqlConnection myConne=conn();
SqlCommand myComm=new SqlCommand(updateStr,myConne);
SqlCommand myComman=new SqlCommand(insertStr,myConne);
try
{
myConne.Open();
myComm.ExecuteNonQuery();
myComman.ExecuteNonQuery();
myConne.Close();
}
================================================
string str= "update student set ticketsNum=(ticketsNum+1) where stuNum= ' "+myStr+ " ';insert into totalCount (voteStuNum,votedStuNum) values( ' " + myString + " ', ' " + myStr + " ') ";
SqlConnection myConne=conn();
SqlCommand myComm=new SqlCommand(str,myConne);
try
{
myConne.Open();
myComm.ExecuteNonQuery();
m