C#如何连接到MS Access 2007
我在连接MS Access DB 2007时遇到问题。代码:C#如何连接到MS Access 2007,c#,C#,我在连接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="+ Direc
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();
}
}
当我运行此代码时,messagebox出现。这意味着插入了我的数据。但是当我打开accdb文件时,没有插入任何数据。我做错了什么
Thnx
编辑:
ExecuteNonQuery()的返回值为1(我编辑我的帖子,因为我无法添加任何评论,当我单击“添加评论”时,该框不会显示..)
编辑2:
我创建了一个具有Title和Location属性的类。代码:
私有void btnSave\u单击(对象发送方,事件参数e)
{
OleDbConnection Conn=新的OleDbConnection()
}
由于我仍然无法单击“添加注释”按钮,下面是我的新代码,其中包含匿名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
ExecuteOnQuery将返回一个int,指示受影响的行数。我要做的第一件事是检查报税表。ExecuteOnQuery可以执行并且不会影响任何不会触发捕获的行。据我所知,您不能将命名参数用于OLEDBPParameter 您的插入应该如下所示:
string sql = "INSERT INTO DVD (titel, locatie)VALUES(?, ?)";
然后您必须以正确的顺序添加OLEDB参数。没有使用这些名称
编辑:
下面是未经测试的代码,但下面是一个示例,说明我将如何执行此操作
using(OleDbConnection connection = new OleDbConnection(CONNECTION_STRING))
{
using(OleDbCommand command = connection.CreateCommand())
{
command.CommandType = CommandType.Text;
command.CommandText = "INSERT INTO DVD(title,locatie)VALUES(?,?)";
command.Parameters.Add("@p1", OleDbType.VarChar, 1).Value = M.Title;
command.Parameters.Add("@p2", OleDbType.VarChar, 255).Value = M.Location;
connection.Open();
int ret = command.ExecuteNonQuery();
}
}
当我编写代码时,我喜欢简化我的工作,并且每次有方法时都不重做。我将为参数创建一个简单的方法,例如:
public void setParameter(String paramAT, String paramTxt)
{
OleDbCommand myCommand;
DbParameter parameter = myCommand.CreateParameter();
parameter.ParameterName = paramAT;
parameter.Value = paramTxt;
myCommand.Parameters.Add(parameter);
}
public int CreateDVD()
{
try
{
string strSqldvd = "INSERT INTO DVD(title,locatie)VALUES(@title,@locate?)";
myCommand = (OleDbCommand)dbconn.MyProvider.CreateCommand();
dbconn.MyConnection.Open();
myCommand.Connection = dbconn.MyConnection;
myCommand.CommandText = strSqldvd;
setParameter("@title",M.Title );
setParameter("@locate", M.Location);
}
catch (Exception)
{
throw new ArgumentException();
}
int count = myCommand.ExecuteNonQuery();
dbconn.MyConnection.Close();
return count;
}
这是多么简单。我在更新和插入中插入并继续使用此参数方法。。。。希望这会有所帮助。无名参数更改是否有帮助?编辑:connection.ExecuteNonQuery();=>命令..ExecuteNonQuery();非常感谢。这帮我省去了很多麻烦!我的更新命令不影响任何内容,返回值为0。
public void setParameter(String paramAT, String paramTxt)
{
OleDbCommand myCommand;
DbParameter parameter = myCommand.CreateParameter();
parameter.ParameterName = paramAT;
parameter.Value = paramTxt;
myCommand.Parameters.Add(parameter);
}
public int CreateDVD()
{
try
{
string strSqldvd = "INSERT INTO DVD(title,locatie)VALUES(@title,@locate?)";
myCommand = (OleDbCommand)dbconn.MyProvider.CreateCommand();
dbconn.MyConnection.Open();
myCommand.Connection = dbconn.MyConnection;
myCommand.CommandText = strSqldvd;
setParameter("@title",M.Title );
setParameter("@locate", M.Location);
}
catch (Exception)
{
throw new ArgumentException();
}
int count = myCommand.ExecuteNonQuery();
dbconn.MyConnection.Close();
return count;
}