我如何使用 Dapper 为我的 sql 语句提供文本框值?
how do i supply textbox values to my sql statement using Dapper?
两次单击按钮的错误消息是:
附加信息:System.Windows.Controls.TextBox 类型的成员 ID 不能用作
一个参数值
private void Button_Click(object sender, RoutedEventArgs e)
{
var connection = new SqlConnection(sqlConnectionString);
Student student = new Student();
connection.Open();
var affectedRows = connection.Execute("Delete from Student Where Id = @Id", new { Id = txtStudentID });
connection.Close();
//return affectedRows;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var connection = new SqlConnection(sqlConnectionString);
Student student = new Student();
var affectedRows = connection.Execute("Update Student set Name = @Name, Marks = @Marks Where Id = @Id", new { Id = txtStudentID, Name = txtName.Text, Marks = txtMarks.Text });
connection.Close();
}
您需要将文本框内的文本作为参数值发送,而不是文本框本身
connection.Execute(
"Delete from Student Where Id = @Id",
new { Id = txtStudentID.Text }
// ^^^^^
);
最好不要让SQL服务器进行数据转换。如果数据库中的ID列是整数,在C#端将字符串解析为整数:
connection.Execute(
"Delete from Student Where Id = @Id",
new { Id = int.Parse(txtStudentID.Text) }
);
或者使用NumbericUpDown控件就不用担心解析失败(只能输入数字)
connection.Execute(
"Delete from Student Where Id = @Id",
new { Id = (int)nudStudentID.Value }
);
同样,如果 ID 是一个Guid,解析它..
更新查询的相同建议 - 您在名称和标记上有 .Text
(它是数字?请参阅上面的解析建议),但在 ID 上没有;可能是 copypasta 错误
其他建议:
您应该写 using var
来创建您的连接。你不需要做一个新的学生。 Dapper 将 open/close 关闭一个连接。它将使您打开的连接保持打开状态。您不使用受影响的行,因此不需要捕获它:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
using var connection = new SqlConnection(sqlConnectionString);
connection.Execute(
"Update Student set Name = @Name, Marks = @Marks Where Id = @Id",
new {
Id = int.Parse(txtStudentID.Text),
Name = txtName.Text,
Marks = double.Parse(txtMarks.Text)
}
);
}
txtStudentID是控件吗?
如果是这样,您应该使用 txtStudentID 的文本 属性。如果它是数字数据类型,那么您可以尝试 .ToString() 它。
两次单击按钮的错误消息是:
附加信息:System.Windows.Controls.TextBox 类型的成员 ID 不能用作 一个参数值
private void Button_Click(object sender, RoutedEventArgs e)
{
var connection = new SqlConnection(sqlConnectionString);
Student student = new Student();
connection.Open();
var affectedRows = connection.Execute("Delete from Student Where Id = @Id", new { Id = txtStudentID });
connection.Close();
//return affectedRows;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var connection = new SqlConnection(sqlConnectionString);
Student student = new Student();
var affectedRows = connection.Execute("Update Student set Name = @Name, Marks = @Marks Where Id = @Id", new { Id = txtStudentID, Name = txtName.Text, Marks = txtMarks.Text });
connection.Close();
}
您需要将文本框内的文本作为参数值发送,而不是文本框本身
connection.Execute(
"Delete from Student Where Id = @Id",
new { Id = txtStudentID.Text }
// ^^^^^
);
最好不要让SQL服务器进行数据转换。如果数据库中的ID列是整数,在C#端将字符串解析为整数:
connection.Execute(
"Delete from Student Where Id = @Id",
new { Id = int.Parse(txtStudentID.Text) }
);
或者使用NumbericUpDown控件就不用担心解析失败(只能输入数字)
connection.Execute(
"Delete from Student Where Id = @Id",
new { Id = (int)nudStudentID.Value }
);
同样,如果 ID 是一个Guid,解析它..
更新查询的相同建议 - 您在名称和标记上有 .Text
(它是数字?请参阅上面的解析建议),但在 ID 上没有;可能是 copypasta 错误
其他建议:
您应该写 using var
来创建您的连接。你不需要做一个新的学生。 Dapper 将 open/close 关闭一个连接。它将使您打开的连接保持打开状态。您不使用受影响的行,因此不需要捕获它:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
using var connection = new SqlConnection(sqlConnectionString);
connection.Execute(
"Update Student set Name = @Name, Marks = @Marks Where Id = @Id",
new {
Id = int.Parse(txtStudentID.Text),
Name = txtName.Text,
Marks = double.Parse(txtMarks.Text)
}
);
}
txtStudentID是控件吗? 如果是这样,您应该使用 txtStudentID 的文本 属性。如果它是数字数据类型,那么您可以尝试 .ToString() 它。