我想问你为什么这段代码不起作用?它没有任何错误,甚至 cmd.ExecuteNonQuery(); 返回1(如果在数据库中更改了一行),但在实际数据库中,绝对没有变化。对于其他数据库表,此代码可以正常工作,但我也无法从该表中删除一行 - 如果是“只读”,它的行为就像它一样,但我不知道为什么 - 昨天,一切正常并且现在,它突然停止工作:-(
string sConnectionString;
sConnectionString = "Data Source=.\\SQLEXPRESS; AttachDbFilename=\"" + zdielaneInfo.Adresar + "\\rozvrh.mdf\";";
sConnectionString += "Integrated Security=True;User Instance=True";
SqlConnection objConn
= new SqlConnection(sConnectionString);
objConn.Open();
SqlCommand cmd = new SqlCommand("", objConn);
if (zdielaneInfo.Edit)
cmd.CommandText = "UPDATE subject " +
"SET name = @name, day = @day, timeStart = @timeStart, timeEnd = @timeEnd "
+ "WHERE id = @id";
else
cmd.CommandText = "INSERT INTO subject (name, day, timeStart, timeEnd) " +
"Values (@name, @day, @timeStart, @timeEnd)";
cmd.Parameters.Add(new SqlParameter("@name", txbName.Text));
cmd.Parameters.Add(new SqlParameter("@day", dniNaInt(cbDen.Text)));
cmd.Parameters.Add(new SqlParameter("@timeStart", DateTime.Parse(txbStart.Text)));
cmd.Parameters.Add(new SqlParameter("@timeEnd", DateTime.Parse(txbEnd.Text)));
cmd.Parameters.Add(new SqlParameter("@id", zdielaneInfo.Id));
cmd.ExecuteNonQuery();
objConn.Close();