Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用c#,net将数据插入access_C#_Ms Access - Fatal编程技术网

使用c#,net将数据插入access

使用c#,net将数据插入access,c#,ms-access,C#,Ms Access,下面是附加的代码,代码运行正常,但它没有将值插入到我创建的access数据库表中,两个代码都没有显示任何错误 namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }

下面是附加的代码,代码运行正常,但它没有将值插入到我创建的access数据库表中,两个代码都没有显示任何错误

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void textBox2_TextChanged(object sender, EventArgs e)
            {
            }

            private void submit_Click(object sender, EventArgs e)                
            {      
                OleDbConnection cnon = new OleDbConnection();
                cnon.ConnectionString =@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\visual_c\Database71.accdb";
                OleDbCommand command = new OleDbCommand();
                command.CommandText = "INSERT INTO electricity (Asset_name,Asset_number)VALUES('" + textBox1.Text + "','" + textBox2.Text + "')";
                cnon.Open();
                command.Connection = cnon;
                command.ExecuteNonQuery();
                cnon.Close();
            }
        }
    }

您在此处缺少一个空间:

"(Asset_name,Asset_number)VALUES"
尝试更改命令字符串:

command.CommandText = "INSERT INTO electricity (Asset_name,Asset_number) VALUES('" + textBox1.Text + "','" + textBox2.Text + "')";
你应该使用它来防止攻击

使用参数化查询 确保您的按钮点击事件

command.CommandText = "INSERT INTO electricity (Asset_name,Asset_number) VALUES (@Assetname,@Assetnumber)",cnon;

command.Parameters.AddWithValue("@Assetname", textBox1.Text);
command.Parameters.AddWithValue("@Assetnumber", textBox2.Text);

“提交”按钮的事件句柄是否已附加到“提交”\u单击?我尝试使用命令参数,但仍不起作用。在我单击Submit之后,它仍然没有显示任何错误,也没有在access中插入任何值。我用参数和新的windows窗体应用程序重试了一次,它成功了。谢谢
command.CommandText = "INSERT INTO electricity (Asset_name,Asset_number) VALUES (@Assetname,@Assetnumber)",cnon;

command.Parameters.AddWithValue("@Assetname", textBox1.Text);
command.Parameters.AddWithValue("@Assetnumber", textBox2.Text);