向 MDB 插入和更新数据
Posted
技术标签:
【中文标题】向 MDB 插入和更新数据【英文标题】:Inserting and Updating data to MDB 【发布时间】:2011-12-24 16:11:58 【问题描述】:我正在尝试制作一个简单的测试程序,它可以打开 MDB 文件并执行 3 项基本操作 MDB 有 3 个字段,它们都是文本:
ID
INFO
TEXT
根据 ID 显示数据 = 得到这个工作
根据 ID 更改数据 = 问题
添加新数据 = 问题
演出数据使用此代码:
con = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = c:\\mdb\\testmdb.mdb");
cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandText = "select Info, text from Table1 where ID = '" + int.Parse(textBox1.Text) + "' ";
con.Open(); // open the connection
OleDbDataReader dr = cmd.ExecuteReader();
while (dr.Read())
textBox2.Text = dr["Info"].ToString();
textBox3.Text = dr["text"].ToString();
con.Close();
如何在 MDB 中插入新数据并更新已有数据?
【问题讨论】:
【参考方案1】:试试这个插入:
con = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = c:\\mdb\\testmdb.mdb");
cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandText = "insert (ID, Info, text) into Table1 values (@ID, @Info, @text);";
cmd.Parameters.AddWithValue("@ID", textBox1.Text);
cmd.Parameters.AddWithValue("@Info", textBox2.Text);
cmd.Parameters.AddWithValue("@text", textBox3.Text);
con.Open(); // open the connection
//OleDbDataReader dr = cmd.ExecuteNonQuery();
cmd.ExecuteNonQuery();
con.Close();
试试这个更新:
con = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = c:\\mdb\\testmdb.mdb");
cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandText = "update Table1 set [Info] = @Info, [text] = @text where ID = @ID;";
cmd.Parameters.AddWithValue("@ID", textBox1.Text);
cmd.Parameters.AddWithValue("@Info", textBox2.Text);
cmd.Parameters.AddWithValue("@text", textBox3.Text);
con.Open(); // open the connection
//OleDbDataReader dr = cmd.ExecuteNonQuery();
cmd.ExecuteNonQuery();
con.Close();
更多操作,请查看this站点的左侧面板。
【讨论】:
现在使用Using
括号可能被认为是最佳实践。另外,您不需要那些数据阅读器,只需致电cmd.ExecuteNonQuery();
错误 2 无法将类型“int”隐式转换为“System.Data.OleDb.OleDbDataReader”
ExecuteNonQuery 的返回值是一个 int,将那些 'OleDbDataReader dr' 行转换为 'int numAffected',它应该可以正常工作
OK 插入工作,但我仍然有更新“UPDATE 语句中的语法错误”的问题。
TEXT 根据this page 似乎是保留字。尝试将text
放入括号中,例如[text]
。【参考方案2】:
工作代码
using System;
using ADOX;
using System.Data.OleDb;
using System.Data;
using System.IO;
namespace ConsoleApplication4
class Program
static void Main(string[] args)
CreateMdb("toster_ru.mdb");
string fileNameWithPath = Environment.CurrentDirectory + "\\toster_ru.mdb";
CreateTableInToMdb(fileNameWithPath);
InsertToMdb(fileNameWithPath);
UpdateToMdb(fileNameWithPath);
var myDataTable = new DataTable();
using (var conection = new OleDbConnection("Provider = Microsoft.JET.OLEDB.4.0; Data Source = " + fileNameWithPath))
conection.Open();
var query = "Select info From my_table";
var adapter = new OleDbDataAdapter(query, conection);
adapter.Fill(myDataTable);
Console.WriteLine(myDataTable.Rows[0][0].ToString()); //output: toster2.ru
Console.ReadKey();
static void CreateMdb(string fileNameWithPath)
if (File.Exists(fileNameWithPath))
return;
Catalog cat = new Catalog();
string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=0;Jet OLEDB:Engine Type=5";
cat.Create(String.Format(connstr, fileNameWithPath));
cat = null;
static void InsertToMdb(string fileNameWithPath)
var con = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " + fileNameWithPath);
var cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandText = "insert into my_table (ID, [Info], [text]) values (@ID, @Info, @text);";
cmd.Parameters.AddWithValue("@ID", 1);
cmd.Parameters.AddWithValue("@Info", "toster.ru");
cmd.Parameters.AddWithValue("@text", "blabla");
con.Open();
cmd.ExecuteNonQuery();
con.Close();
static void UpdateToMdb(string fileNameWithPath)
var con = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " + fileNameWithPath);
var cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandText = "UPDATE my_table SET [Info] = ?, [text] = ? WHERE ID = ?;";
cmd.Parameters.AddWithValue("@p1", OleDbType.VarChar).Value = "toster2.ru";
cmd.Parameters.AddWithValue("@p2", OleDbType.VarChar).Value = "blabla2";
cmd.Parameters.AddWithValue("@p3", OleDbType.VarNumeric).Value = 1;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
static void CreateTableInToMdb(string fileNameWithPath)
try
OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + fileNameWithPath);
myConnection.Open();
OleDbCommand myCommand = new OleDbCommand();
myCommand.Connection = myConnection;
myCommand.CommandText = "CREATE TABLE my_table([ID] NUMBER, [Info] TEXT, [text] TEXT)";
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
catch
【讨论】:
以上是关于向 MDB 插入和更新数据的主要内容,如果未能解决你的问题,请参考以下文章