[英]update database using SqlDataAdapter in C#
I have below code to update my database table when button is clicked but it doesn't work. 当单击按钮时,我有下面的代码更新我的数据库表,但是它不起作用。
protected void Button_Click(object sender, EventArgs e)
{
HasinReservation.Entities.Db.Transaction dt = new Transaction();
SqlConnection connection = new SqlConnection(
@"Data Source=192.x.x.x\Sql2008;Initial Catalog=GardeshgariKish;User ID=cms;Password=xxxxx;MultipleActiveResultSets=True;Application Name=EntityFramework");
connection.Open();
SqlCommand sqlCmd = new SqlCommand(
"Update Transaction SET IsCancelled = 1 WHERE BarCodeNumber = @Value1", connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
string barcode = dgvData.Rows[0].Cells[12].Text;
sqlCmd.Parameters.AddWithValue("Value1", barcode);
connection.Close();
}
I am troubled by your implementation of Entity Framework but then not using the framework for what it was designed... 我为您实施Entity Framework感到困扰,但随后并未将其用于设计的框架...
You have configured your data adapter and the command, and even opened the connection... but you have not actually executed the command. 您已经配置了数据适配器和命令,甚至打开了连接...但是您尚未实际执行命令。
sqlCmd.ExecuteNonQuery();
I understand that your actual business logic may have been replaced with this simple CRUD operation, but the main reason that we use the Entity Framework is to avoid writing any T-SQL in our business logic. 我了解您的实际业务逻辑可能已被此简单的CRUD操作所取代,但是我们使用实体框架的主要原因是避免在业务逻辑中编写任何T-SQL。 Why didn't you use the framework to commit the change:
您为什么不使用该框架来提交更改:
protected void Button3_Click(object sender, EventArgs e)
{
// cancel the selected transaction
string selectedBarcode = dgvData.Rows[0].Cells[12].Text;
using(var dataContext = new HasinReservation.Entities.Db())
{
var transaction = dataContext.Transaction.Single(t => t.Barcode == selectedBarcode);
transaction.IsCancelled = true;
dataContext.SaveChanges();
}
}
This in itself may not be a great solution but it uses the framework to do exactly what you attempted to do manually. 这本身可能不是一个很好的解决方案,但是它使用框架来完全执行您尝试手动执行的操作。
Why are you trying to use a SqlDataAdapter
to execute an UPDATE
statement? 为什么要尝试使用
SqlDataAdapter
执行UPDATE
语句? When constructing a SqlDataAdapter
with a SqlCommand
object, that command object represents the SELECT
command for the adapter. 当使用
SqlCommand
对象构造SqlDataAdapter
时,该命令对象表示适配器的SELECT
命令。 An UPDATE
statement doesn't select anything, and a SELECT
command doesn't update anything. UPDATE
语句不选择任何内容,而SELECT
命令不更新任何内容。
Get rid of the SqlDataAdapter
entirely and just execute the command: 完全摆脱
SqlDataAdapter
并执行以下命令:
sqlCmd.ExecuteNonQuery();
You'll probably also want to add some error handling so exceptions don't reach the UI (and to ensure the connection is properly closed on error conditions). 您可能还需要添加一些错误处理,以使异常不会到达UI(并确保在错误情况下正确关闭了连接)。 You also don't seem to be doing anything with that
Transaction
object, so you can probably get rid of that too. 您似乎也没有对该
Transaction
对象做任何事情,因此您也可以摆脱它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.