在数据库中保存数据(Microsoft Access) [英] Saving data in the database(Microsoft Access)
问题描述
我正在尝试创建一个拥有数据库的项目(Microsoft Access)我可以使用Windows窗体应用程序读取数据库中的内容,但我真的无法理解如何在数据库中保存数据,谁能给我一个关于OleDB的示例项目?我真的在谷歌找不到一个好的指南:(
这里是我在数据库中读取数据的代码,我打算制作一个注册表格,保存数据在文本框中:)谢谢!
I''m trying to make a project that has a database(Microsoft Access) I can read what''s on the database using Windows forms application but I really can''t understand how to save data in the database, Can anyone give me a sample project regarding OleDB? I really can''t find a good guide in the google :(
here is my code for reading data in the database, and I plan to make a register form, that saves data in the textbox :) thanks!
OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\sisc-erelim\4_Printing\VTDB\DB\VirginiTEADB2.accdb");
private void Button1_Click(object sender, EventArgs e)
{
cn.Open();
OleDbDataReader oreader = null;
OleDbCommand commando = new OleDbCommand("select* from Accountstbl", cn);
oreader = commando.ExecuteReader();
while (oreader.Read())
{
user.Add(oreader["Username"].ToString());
pass.Add(oreader["Password"].ToString());
rights.Add(oreader["Rights"].ToString());
}
cn.Close();
int totalItems = user.Count;
int count = 0;
string isValidated = "";
while (count < totalItems)
{
if ((textBox1.Text == user[count].ToString()) && (textBox2.Text == pass[count].ToString()))
{
isValidated = "True";
rights2 = rights[count].ToString();
count = 100;
}
else
{
count++;
}
}
if (isValidated == "True")
{
this.Hide();
Form2 posForm = new Form2();
posForm.ShowDialog();
textBox1.Clear();
textBox2.Clear();
}
else
{
MessageBox.Show("Username and Password do not match! Please try again!", "ERROR",MessageBoxButtons.OK ,MessageBoxIcon.Error);
}
}
推荐答案
使用这样的Insert语句。这只是用实际表格和字段名称替换表格和字段名称的示例
Use the Insert statement like this. This is just a sample replace the Table and field names with your actual table and field names
OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\sisc-erelim\4_Printing\VTDB\DB\VirginiTEADB2.accdb");
string myExecuteQuery = "INSERT INTO TABLE_NAME (FIELD1,FIELD2) VALUES (VALUE1,VALUE2)";
OleDbCommand myCommand = new OleDbCommand(myExecuteQuery, cn);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
如果要更新现有值,请使用UPDATE命令而不是INSERT INTO。
If you want to update the existing value, use UPDATE command instead of INSERT INTO.
没有充分的理由继续使用访问权限,因为你可以下载下载带有SP1的SQL Server 2012 Express [ ^ ]。
20年前,当你不得不放弃一些严肃的资金来获得一个严肃的数据库服务器时,访问是很好的。
CP上有很多很多文章,它们向你展示了如何用SQL服务器做你想做的事。
最好的问候
Espen Harlinn
There really are no good reasons to use access anymore, as you can download Download SQL Server 2012 Express with SP1[^].
Access was kind of nice about 20 years ago - when you had to part with some serious money to get a serious database server.
And there are many, many articles here on CP, that shows you how to do what you want with SQL server.
Best regards
Espen Harlinn
这篇关于在数据库中保存数据(Microsoft Access)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!