将数据插入和更新到MDB

时间:2011-12-24 16:11:59

标签: c# ms-access

我正在尝试制作一个简单的测试程序,可以打开MDB文件并执行3个基本操作 MDB有 3个字段,所有字段都是文本:

ID
INFO 
TEXT
  • 根据ID =显示数据=得到此工作
  • 根据ID =问题
  • 更改数据
  • 添加新数据=问题

show data使用此代码:

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中插入新数据并更新我已有的数据?

2 个答案:

答案 0 :(得分: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网站的左侧面板。

答案 1 :(得分:1)

工作代码

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 { }
    }
}
}