gpt4 book ai didi

C# 如何连接到 MS Access 2007

转载 作者:太空狗 更新时间:2023-10-29 23:42:44 24 4
gpt4 key购买 nike

我在连接到我的 MS Access DB 2007 时遇到问题。代码:

private void btnSave_Click(object sender, EventArgs e)
{
OleDbConnection Conn = new OleDbConnection();

try
{
string conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+ Directory.GetCurrentDirectory() +"\\dvd_manager.accdb;Persist Security Info=False;";
Conn.ConnectionString = conn;

Conn.Open();

int i = cbbLocatie.SelectedIndex + 65;
char c = (char)i;

string sql = "INSERT INTO DVD (titel, locatie)VALUES(@titel, @locatie)";
OleDbCommand Com = new OleDbCommand();
Com.CommandText = sql;
Com.Connection = Conn;

OleDbParameter Param = new OleDbParameter("@titel", txtTitle.Text);
Com.Parameters.Add(Param);

Param = new OleDbParameter("@locatie", c);
Com.Parameters.Add(Param);

Com.ExecuteNonQuery();
Conn.Close();

MessageBox.Show("Data is opgeslagen " + sql);
}
catch (Exception ex)
{
MessageBox.Show("Fout opgetreden: " + ex.Message);
}
finally
{
Conn.Close();
}
}

当我运行这段代码时,会出现消息框。这应该意味着我的数据已插入。但是当我打开 accdb 文件时,没有插入任何数据。我做错了什么?

感谢

编辑:ExecuteNonQuery() 的返回值为 1(我编辑了我的帖子,因为我无法添加任何评论,当我点击添加评论时,该框没有显示..)

编辑 2:我创建了一个具有 Title 和 Location 属性的类。代码: private void btnSave_Click(object sender, EventArgs e){ OleDbConnection Conn = new OleDbConnection();

try
{
string conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+ Directory.GetCurrentDirectory() +"\\dvd_manager.accdb;Persist Security Info=False;";
Conn.ConnectionString = conn;

// Create object
Medium M = new Medium();
int i = cbbLocatie.SelectedIndex + 65;
char c = (char)i;

M.Location = c;
M.Title = txtTitle.Text;

Conn.Open();

string sql = "INSERT INTO DVD (titel, locatie)VALUES(@titel, @locatie)";
OleDbCommand Com = new OleDbCommand();
Com.CommandText = sql;
Com.Connection = Conn;

OleDbParameter Param1 = new OleDbParameter("@titel", M.Title);
Com.Parameters.Add(Param1);

OleDbParameter Param2 = new OleDbParameter("@locatie", M.Location);
Com.Parameters.Add(Param2);

int ret = Com.ExecuteNonQuery();
Conn.Close();

MessageBox.Show("Data is opgeslagen " + ret);
}
catch (OleDbException ex)
{
MessageBox.Show(ex.Message);
}
catch (Exception ex)
{
MessageBox.Show("Fout opgetreden: " + ex.Message);
}
finally
{
Conn.Close();
}

因为我仍然无法点击添加评论按钮,这里是我的带有无名 sql 参数的新代码:

// some code
Conn.Open();

string sql = "INSERT INTO DVD (titel, locatie)VALUES(?, ?)";
OleDbCommand Com = new OleDbCommand();
Com.CommandText = sql;
Com.Connection = Conn;

OleDbParameter Param1 = new OleDbParameter("@p1", OleDbType.VarChar, 1);
Param1.Value = M.Title;
Com.Parameters.Add(Param1);

OleDbParameter Param2 = new OleDbParameter("@p2", OleDbType.VarChar, 255);
Param2.Value = M.Location;
Com.Parameters.Add(Param2);

int ret = Com.ExecuteNonQuery();
Conn.Close();
// morde code

最佳答案

ExecuteNonQuery 将返回一个 int,指示受影响的行数。我要做的第一件事是检查返回。 ExecuteNonQuery 可以执行而不影响任何行,不会触发捕获。

关于C# 如何连接到 MS Access 2007,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/508345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com