如何正确初始化 .NET OleDbConnection 对象及其 ConnectionString 属性?

发布于 2024-12-29 07:45:35 字数 494 浏览 3 评论 0原文

这是我用于连接和使用 Access 数据库的 C# 代码。

using System.Data.OleDb;

var cb = new OleDbCommandBuilder(da);

DataRow dRow = ds1.Tables["Customer"].NewRow();

dRow[0] = textBox1.Text;
dRow[1] = textBox2.Text;
dRow[2] = textBox3.Text;

ds1.Tables["Customer"].Rows.Add(dRow);

da.Update(ds1, "Customer");

con.Close();

MessageBox.Show("Entry added");


但是 da.Update(ds1,"Customer"); 行抛出异常:

ConnectionString 属性尚未初始化。

This is my C# code to connect and work with an Access database.

using System.Data.OleDb;

var cb = new OleDbCommandBuilder(da);

DataRow dRow = ds1.Tables["Customer"].NewRow();

dRow[0] = textBox1.Text;
dRow[1] = textBox2.Text;
dRow[2] = textBox3.Text;

ds1.Tables["Customer"].Rows.Add(dRow);

da.Update(ds1, "Customer");

con.Close();

MessageBox.Show("Entry added");

But the line da.Update(ds1,"Customer");, throws an exception:

The ConnectionString property has not been initialized.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

じ违心 2025-01-05 07:45:35

我不太明白你的问题,但这里有一些示例代码可以帮助你弄清楚你想要做什么。

为了清楚起见:数据库名为“MyDb.accdb”,并有一个名为“Customer”的表,其中包含两个字段“Name”和“Phone”。此示例假设数据库与可执行文件位于同一目录中。

private void AddCustomer(string customerName, string customerPhone)
{
    string name = customerName;
    string phone = customerPhone;

    // An easy way to determine the connection string to your database is to open the database from Visual Studio's 'Server Explorer'.
    // Then, from Server Explorer, view the Properties of the database - in the Properties you will see the "Connection String". 
    // You can/should replace the arbitrary part of the path with "|DataDirectory|".
    string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|MyDb.accdb;Persist Security Info=True";

    // Create your sql query in a string variable
    string cmdText = string.Format("INSERT INTO Customer(Name, Phone) VALUES('{0}','{1}');", name, phone);

    // Use the 'using' statement on your connection so that the resource is managed properly
    using (System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection(connString))
    {                
        // Here's where/how we fire off the INSERT statement
        OleDbCommand cmd = new OleDbCommand(cmdText, connection);
        connection.Open();
        cmd.ExecuteNonQuery();
    }
}

I'm not following your question too well, but here's some sample code that may help you figure out whatever it is that you are trying to do.

For clarity: The database is named "MyDb.accdb" and has a table named "Customer" which has two fields "Name" and "Phone". This example assumes the database lives in the same directory as the executable.

private void AddCustomer(string customerName, string customerPhone)
{
    string name = customerName;
    string phone = customerPhone;

    // An easy way to determine the connection string to your database is to open the database from Visual Studio's 'Server Explorer'.
    // Then, from Server Explorer, view the Properties of the database - in the Properties you will see the "Connection String". 
    // You can/should replace the arbitrary part of the path with "|DataDirectory|".
    string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|MyDb.accdb;Persist Security Info=True";

    // Create your sql query in a string variable
    string cmdText = string.Format("INSERT INTO Customer(Name, Phone) VALUES('{0}','{1}');", name, phone);

    // Use the 'using' statement on your connection so that the resource is managed properly
    using (System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection(connString))
    {                
        // Here's where/how we fire off the INSERT statement
        OleDbCommand cmd = new OleDbCommand(cmdText, connection);
        connection.Open();
        cmd.ExecuteNonQuery();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文