【发布时间】:2012-05-15 19:01:40
【问题描述】:
我正在尝试从 C# 更新 access 数据库中的表。
它是一个.mdb类型的数据库。
这是我正在使用的连接字符串。
public MainWindow() {
InitializeComponent();
OleDbConnection cn= new OleDbConnection();}
cn.ConnectionString=@"Provider=Microsoft.Jet.OLEDB.4.0;DataSource="
+ "C:\Users\Angela\Documents\Visual Studio 2010\Projects\Havnefoged11.mdb;"
+ "User Id=;Password=;";
cn.Open();
Application.Current.Properties["DBConnection"] = cn;
文件位于正确的文件夹 C:\Users\Angela\Documents\Visual Studio 2010\Projects
private void button1_Click_1(object sender, RoutedEventArgs e)
{
OleDbConnection conn =
(OleDbConnection)Application.Current.Properties["DBConnection"];
//Below are the values i want to put into the database
String dybde = Dybde.Text;
String bredde = Bredde.Text;
String plads = PladsNummer.Text;
String StrSQL =
"INSERT INTO Bådpladser (Plads_nummer,Bredde,Dybde) VALUES (´´"+ plads
+ "´,´"
+ bredde
+ "´,"
+ dybde+");";
OleDbCommand InsertCommand = new OleDbCommand(StrSQL, conn);
InsertCommand.ExecuteNonQuery();
}
然后我得到错误 OleDBException is unhandled NO value given for a or more parameters
我已经进去把数据库中的所有字段都改成了文本 我检查了它们是否正确的字段名称。 ldb 文件在我初始化程序时出现。 但是当我按下按钮时,我得到了错误。
有什么想法吗?
附言 我已经包含了
using System.Data;
using System.Data.OleDb;
【问题讨论】:
-
请以可读的方式格式化您的代码..!
-
你有一个 SQL 注入漏洞。
-
dybde的价值是什么?我敢打赌它是一个空字符串,或者为空。此外,您在第一个值之前还有一个额外的单引号。最后,通过连接用户输入来构建 SQL 字符串是一种可怕的做法。你向 SQL 注入敞开心扉。您应该改用参数化查询。 (如果你用谷歌搜索“Parameterized Queries .NET”,会有大量信息)
-
SQL 注入:Exploits of a Mom