将数据插入和更新到MDB
我正在尝试制作一个简单的测试程序,可以打开MDB文件并做3个基本的事情,MDB有3个字段 ,所有这些都是文本:
ID INFO TEXT
show数据与此代码一起使用:
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中插入新数据并更新我已有的数据?
试试这个插入:
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();
有关更多操作,请检查此站点的左侧面板。
工作代码
上述就是C#学习教程:将数据插入和更新到MDB分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
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 { } } } }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1004468.html