這些是我的函數來更新SQL記錄庫中的學生記錄和插入學生記錄。
插入函數插入SQL表中的記錄正常工作,但更新函數更新記錄不能正常工作
public void UpdateStudent(ref student stu, string rollno) //Update the values to corresponding roll number 'rollno'
{
try
{
connection.Open(); //I have already defined the connection and command
command = new SqlCommand("update student set FirstName='"+stu.Firstname+"',LastName='"+stu.Lastname+"',YearOfAdmission="+stu.Yearofadmission+",Branch='"+stu.Branch+"' where RollNo='"+rollno+"'", connection); //Yearofadmission is int
command.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
if (connection != null)
connection.Close();
}
}
public void insertstudent(ref student s)
{
try
{
connection.Open();
command = new SqlCommand("insert into student values('"+s.Rollno+"','"+ s.Firstname+"','"+s.Lastname+"',"+s.Yearofadmission+",'"+s.Branch+"','"+s.Password+"')", connection);
command.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
if (connection != null)
connection.Close();
}
}
我的第二個功能「insertstudent」插入值到SQL表工作正常並正確插入值到數據庫表。但第一個功能'更新學生'沒有更新數據庫表中的值。它也沒有提供任何錯誤。 所以我錯了?
在此先感謝!
您應該使用命名參數而不是連接sql字符串。 –
嘗試捕獲更新中生成的SQL,並在Management Studio中運行它,看看它做了什麼..看起來像你的過濾器rollno不是以某種方式匹配的。 – AJP
確保切換到sql參數!如果你真的想看到這個問題,我會在那裏放置一個斷點並進行調試。比檢查你的命令,看看它在做什麼。但是,是的,廢除代碼並切換到http://www.dotnetperls.com/sqlparameter –