嗨,我想在 Windows C# 中通过 dapper 创建一个简单的笔记本 它连接到sql 运行良好 但在我添加一个新项目后 它有一个错误 System.NotSupportedException:“System.Windows.Forms.TextBox 类型的成员 jdid 不能用作参数值” jdid 是文本框名称
命名空间 Faradarss { 公共部分类 Form1 :表格 { 公共表格1() { 初始化组件(); }
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["faradarss"].ToString();
private void btnsave_Click(object sender, EventArgs e)
{
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
int affectedRow = connection.Execute("INSERT INTO tblfaradars (id,sen,esm,jdid) VALUES (@id ,@sen ,@esm,@jdid)", new { id=txtid.Text, sen=txtsen.Text,esm=txtesm.Text ,jdid=textBox1 });
if (affectedRow != 0)
{
MessageBox.Show("ردیف ساخت");
}
connection.Close();
}
}
这是一个插入记录并返回新标识符的示例,其中标识符是自动递增主键。
id 列通常是数据库处理递增的主键,当它出现在您设置 id 的代码中时,因此请考虑使 id 列自动递增。
请注意,Dapper 处理打开和关闭连接。
using Dapper;
using Microsoft.Data.SqlClient;
namespace WinFormsApp1;
public partial class Form1 : Form
{
private static string _connectionString =
"Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=InsertExamples;Integrated Security=True;Encrypt=False";
public Form1()
{
InitializeComponent();
}
private void AddButton_Click(object sender, EventArgs e)
{
Customer customer = new Customer
{
FirstName = FirstNameTextBox.Text,
LastName = LastNameTextBox.Text,
Active = ActiveCheckBox.Checked
};
var statement =
"INSERT INTO dbo.Customer (FirstName,LastName,Active) " +
"VALUES (@FirstName, @LastName, @Active); SELECT CAST(scope_identity() AS int);";
using (var cn = new SqlConnection(_connectionString))
{
customer.Id = cn.QueryFirst<int>(statement, customer);
MessageBox.Show($"Customer id is {customer.Id}");
}
}
}
// place in its own file.
public class Customer
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool? Active { get; set; }
}