如何使用C#在ms访问中保存数据 [英] How to save data in ms access using C#

查看:77
本文介绍了如何使用C#在ms访问中保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

connection.Open();
               OleDbCommand command = new OleDbCommand();
               command.Connection = connection;
               command.CommandText = "insert into tblcustomer where customername='" + txt_customername.Text + "' and Name='" + txt_Name.Text + "' and Surname='" + txt_Surname.Text + "'";

               command.ExecuteNonQuery();
               MessageBox.Show("Data Saved");
               connection.Close();





我的尝试:



i已添加新数据,但他没有保存我的访问表,所以请帮助我



What I have tried:

i have add new data but he not save my access table so please help me

推荐答案

您确定需要一个 WHERE INSERT 语句中的子句?另外,不要使用字符串连接来构建SQL命令,它可能会导致数据丢失;请参阅 bobby-tables.com:防止SQL注入的指南 [ ^ ]。



另外,不要显示 数据已保存,当您未检查SQL命令的结果时。
Are you sure you need a WHERE clause on an INSERT statement? Also, do not use string concatenation to build SQL commands, it can lead to the loss of your data; see bobby-tables.com: A guide to preventing SQL injection[^].

Also, do not display messages like "Data Saved", when you are not checking the results of your SQL commands.


command.CommandText = "insert into tblcustomer where customername='" + txt_customername.Text + "' and Name='" + txt_Name.Text + "' and Surname='" + txt_Surname.Text + "'";



不是你问题的解决方案,而是你遇到的另一个问题。

永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

按示例进行SQL注入攻击 [ ^ ]

PHP:SQL注入 - 手册 [ ^ ]

SQL注入预防备忘单 - OWASP [ ^ ]


Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]


首先,关于 SqlInjection 的所有注释都非常重要!所以,请在开始做任何事之前先阅读它。



而不是这样的陈述:

First of all, all notes about SqlInjection are very important! So, please, read about it before you start doing anything.

Instead of such of statement:
command.CommandText = "insert into tblcustomer where customername='" + txt_customername.Text + "' and Name='" + txt_Name.Text + "' and Surname='" + txt_Surname.Text + "'";



你应该使用参数化查询:


you should use parameterized query:

command.CommandText = "INSERT INTO tblcustomer (customername, Name, Surname)
VALUES(@custname, @name, @surname)";
command.Parameters.AddWithValue("@custname", txt_customername.Text);
command.Parameters.AddWithValue("@name", txt_Name.Text);
command.Parameters.AddWithValue("@surname", txt_Surname.Text);





欲了解更多详情,请参阅:

OleDbParameterCollection .AddWithValue方法(字符串,对象)(System.Data.OleDb) [ ^ ]


这篇关于如何使用C#在ms访问中保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆