简体   繁体   English

连接到 Access 数据库

[英]Connecting to Access database

I'm trying to connect to database file "crepeDB.accdb"我正在尝试连接到数据库文件“crepeDB.accdb”

When I added it through data connection, and works fine when I drag any table to appear as data grid in any form but when I try to connect to the database to insert data it gives me this error:当我通过数据连接添加它时,当我拖动任何表以任何形式显示为数据网格时工作正常,但是当我尝试连接到数据库以插入数据时,它给了我这个错误:

An unhandled exception of type 'System.NotImplementedException' occurred in Additional information: The method or operation is not implemented.附加信息中发生类型为“System.NotImplementedException”的未处理异常:未实现该方法或操作。

The code I'm using is as follows:我使用的代码如下:

System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
// TODO: Modify the connection string and include any
// additional required properties for your database.
conn.ConnectionString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data source=|DataDirectory|\\crepeDB.accdb;");

conn.Open();
string query = "insert into Sales (Sdate,SQuantity) values ('" + dateTimePicker1.Value + "','" + textBox9.Text + "')";
OleDbCommand cmd = new OleDbCommand(query, conn);
cmd.ExecuteNonQuery();

This is the last thing I need to do in my project, would really appreciate any help.这是我需要在我的项目中做的最后一件事,非常感谢任何帮助。

Do not pass values for your fields concatenating them to form your command, instead use parameters.不要为您的字段传递值,将它们连接起来形成您的命令,而是使用参数。

int quantity;
if(!Int32.TryParse(textBox9.Text, out quantity))
     MessageBox.Show("Invalid number");
else
{
    using(OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data source=|DataDirectory|\\crepeDB.accdb;"))
    {    
        conn.Open();
        string query = @"insert into Sales (Sdate,SQuantity) 
                         values (@date, @qta)";
        OleDbCommand cmd = new OleDbCommand(query, conn);
        cmd.Parameters.Add("@date", OleDbType.Date).Value =  dateTimePicker1.Value;
        cmd.Parameters.Add("@qta", OleDbType.Integer).Value = quantity;
        cmd.ExecuteNonQuery();
    }
}

This is better because you don't ask someone else to convert your values from a string to the correct datatype.这更好,因为您不会要求其他人将您的值从字符串转换为正确的数据类型。 This automatic conversion (in particular with dates) is well know to cause problems when there is some kind of mismatch between the passed string and how the database engine interprets this string众所周知,当传递的字符串与数据库引擎如何解释此字符串之间存在某种不匹配时,这种自动转换(尤其是日期)会导致问题

NB I am assuming the Sdate is a field of type DateTime and SQuantity is a field of type Integer in MS-Access.注意,我假设 Sdate 是一个 DateTime 类型的字段,SQuantity 是 MS-Access 中一个 Integer 类型的字段。 If not then you can change the OleDbType Int32.TryParse to the correct matching type如果没有,那么您可以将 OleDbType Int32.TryParse 更改为正确的匹配类型

It is basically like this .基本上就是这样。 . . . .

con.Open();
SqlCommand cmd = new SqlCommand(@"insert into tbl_insert values(@name,@email,@add)", con);
cmd.Parameters.AddWithValue("@name", txtname.Text);
cmd.Parameters.AddWithValue("@email", txtemail.Text);
cmd.Parameters.AddWithValue("@add", txtadd.Text);
cmd.ExecuteNonQuery();
con.Close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM