使用 c# 和 ms access 进行编辑
我有一个问题,我尝试了这段代码:
private void button1_Click(object sender, RoutedEventArgs e)
{
conn.Open();
OleDbCommand cmd = new OleDbCommand("UPDATE tbl_Fullname SET Firstname=@firstn,Lastname=@lastn,Middlename=@midn WHERE fnID=@idn", conn);
cmd.Parameters.Add("@idn", OleDbType.VarChar).Value = textBox5.Text;
cmd.Parameters.Add("@firstn", OleDbType.VarChar).Value = textBox1.Text;
cmd.Parameters.Add("@lastn", OleDbType.VarChar).Value = textBox2.Text;
cmd.Parameters.Add("@midn", OleDbType.VarChar).Value = textBox3.Text;
cmd.ExecuteNonQuery();
conn.Close();
}
没有错误,但是当我检查我的数据库访问权限时,没有任何变化。
这是更新和编辑的确切代码吗?
这是我的整个代码:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
private void button3_Click(object sender, RoutedEventArgs e)
{
OleDbCommand cmd = new OleDbCommand("INSERT INTO tbl_Fullname (Lastname,Firstname,Middlename) VALUES (@first,@last,@midn)",conn);
conn.Open();
cmd.Parameters.Add("@first", OleDbType.VarChar).Value = textBox1.Text;
cmd.Parameters.Add("@last", OleDbType.VarChar).Value = textBox2.Text;
cmd.Parameters.Add("@midn", OleDbType.VarChar).Value = textBox3.Text;
cmd.ExecuteNonQuery();
conn.Close();
}
private void textBox4_TextChanged(object sender, TextChangedEventArgs e)
{
conn.Open();
OleDbCommand cmd2 = new OleDbCommand("SELECT fnID,Lastname,Firstname,Middlename FROM tbl_Fullname WHERE fnID= @id", conn);
cmd2.Parameters.Add("@id", OleDbType.VarChar).Value = textBox4.Text;
try
{
OleDbDataReader dr = cmd2.ExecuteReader();
if (dr.Read())
{
textBox1.Text = dr[1].ToString();
textBox2.Text = dr[2].ToString();
textBox3.Text = dr[3].ToString();
textBox5.Text = dr[0].ToString();
}
else
{
MessageBox.Show("No result");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
conn.Close();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
conn.Open();
OleDbCommand cmd = new OleDbCommand("UPDATE tbl_Fullname SET Firstname=@firstn,Lastname=@lastn,Middlename=@midn WHERE fnID=@idn", conn);
cmd.Parameters.Add("@idn", OleDbType.VarChar).Value = textBox5.Text;
cmd.Parameters.Add("@firstn", OleDbType.VarChar).Value = textBox1.Text;
cmd.Parameters.Add("@lastn", OleDbType.VarChar).Value = textBox2.Text;
cmd.Parameters.Add("@midn", OleDbType.VarChar).Value = textBox3.Text;
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
我希望有人可以帮助我。我非常需要它。先感谢您。
i have a problem, i tried this code :
private void button1_Click(object sender, RoutedEventArgs e)
{
conn.Open();
OleDbCommand cmd = new OleDbCommand("UPDATE tbl_Fullname SET Firstname=@firstn,Lastname=@lastn,Middlename=@midn WHERE fnID=@idn", conn);
cmd.Parameters.Add("@idn", OleDbType.VarChar).Value = textBox5.Text;
cmd.Parameters.Add("@firstn", OleDbType.VarChar).Value = textBox1.Text;
cmd.Parameters.Add("@lastn", OleDbType.VarChar).Value = textBox2.Text;
cmd.Parameters.Add("@midn", OleDbType.VarChar).Value = textBox3.Text;
cmd.ExecuteNonQuery();
conn.Close();
}
there was no error but when i ckecked my db access there is nothing changed.
is this the exact code for updatint and editing?
here is my whole code:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
private void button3_Click(object sender, RoutedEventArgs e)
{
OleDbCommand cmd = new OleDbCommand("INSERT INTO tbl_Fullname (Lastname,Firstname,Middlename) VALUES (@first,@last,@midn)",conn);
conn.Open();
cmd.Parameters.Add("@first", OleDbType.VarChar).Value = textBox1.Text;
cmd.Parameters.Add("@last", OleDbType.VarChar).Value = textBox2.Text;
cmd.Parameters.Add("@midn", OleDbType.VarChar).Value = textBox3.Text;
cmd.ExecuteNonQuery();
conn.Close();
}
private void textBox4_TextChanged(object sender, TextChangedEventArgs e)
{
conn.Open();
OleDbCommand cmd2 = new OleDbCommand("SELECT fnID,Lastname,Firstname,Middlename FROM tbl_Fullname WHERE fnID= @id", conn);
cmd2.Parameters.Add("@id", OleDbType.VarChar).Value = textBox4.Text;
try
{
OleDbDataReader dr = cmd2.ExecuteReader();
if (dr.Read())
{
textBox1.Text = dr[1].ToString();
textBox2.Text = dr[2].ToString();
textBox3.Text = dr[3].ToString();
textBox5.Text = dr[0].ToString();
}
else
{
MessageBox.Show("No result");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
conn.Close();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
conn.Open();
OleDbCommand cmd = new OleDbCommand("UPDATE tbl_Fullname SET Firstname=@firstn,Lastname=@lastn,Middlename=@midn WHERE fnID=@idn", conn);
cmd.Parameters.Add("@idn", OleDbType.VarChar).Value = textBox5.Text;
cmd.Parameters.Add("@firstn", OleDbType.VarChar).Value = textBox1.Text;
cmd.Parameters.Add("@lastn", OleDbType.VarChar).Value = textBox2.Text;
cmd.Parameters.Add("@midn", OleDbType.VarChar).Value = textBox3.Text;
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
i hope someone can help me. i need it badly. thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MSDN 说:
因此,请这样做:
请注意,参数的顺序在这里很重要!
MSDN says:
So make it that way:
Note that order of parameters is important here!
在 SQL 周围添加 try/catch 并输出 sqlCommand 异常。
另请参阅此链接 http://idealprogrammer。 com/net-languages/code-samples/sql-command-update-statement-source-code/ 我发现你可以在哪里看到如何插入、更新等。适当地。
Add a try/catch around your SQL and output the sqlCommand Exeption.
Also see this link http://idealprogrammer.com/net-languages/code-samples/sql-command-update-statement-source-code/ I found where you can see how to insert,update,etc. properly.