从文本框中更新Access数据库时出现问题 [英] Problems updating Access database from textbox

查看:101
本文介绍了从文本框中更新Access数据库时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我进行了搜索,但没有任何反应,我只是开始使用C#.NET,并且在表单上有一个文本框.我从数据库中检索了一些数据,并通过组合框显示到一个文本框,该框指示了我要显示的部分(我已经这样做了!).
但是,当我尝试更新不起作用时,我单击我的按钮以更新访问数据库(Access 2007),但没有任何反应,用户只是更改了某些内容,并且该按钮必须更新acces数据库,希望您能为我提供帮助:D
到目前为止,这是我的代码:

Hello everyone
I have searched, but nothing happens, I just starting using C#.NET and I a have a textbox on a form. I retrieve some data from the database and I display to a textbox through a combobox that indicate the section I want to display (I already do this!).
But when I try to update nothing works, I click my button to update the access database(Access 2007) and nothing happens, the user just changes something and the button has to update the acces database, I hope you can help me :D
this is my code so far:

String contenido = textBox3.Text;
     String nombre = comboBox2.SelectedValue.ToString();
     using (var conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=database.accdb"))
     using (var cmd = conn.CreateCommand())
     {

         cmd.CommandText = "UPDATE [Seccion] SET [contenido] ="+contenido+ "WHERE [nombre] =" +nombre;
         cmd.Parameters.AddWithValue("@contenido",contenido);
         cmd.Parameters.AddWithValue("@nombre", nombre);

         conn.Open();
         cmd.ExecuteNonQuery();

         int rowsAffected = cmd.ExecuteNonQuery();
         if (rowsAffected == 1)
         {
             MessageBox.Show("Success");
         }
         else
         {
             MessageBox.Show(string.Format("{0} Rows Affected", rowsAffected));
         }

推荐答案

cmd.ExecuteNonQuery();

int rowsAffected = cmd.ExecuteNonQuery();



为什么要两次调用ExecuteNonQuery?

您还需要详细说明什么都不会发生".是否所有时间和活动都停止了?宇宙会冻结吗?



Why are you calling ExecuteNonQuery twice?

You will also need to elaborate on "nothing happens". Does all time and activity stop? Does the universe freeze?


尝试更改:
Try changing:
cmd.CommandText = "UPDATE [Seccion] SET [contenido] ="+contenido+ "WHERE [nombre] =" +nombre;
  cmd.Parameters.AddWithValue("@contenido",contenido);
  cmd.Parameters.AddWithValue("@nombre", nombre);



至:



to:

cmd.CommandText = "UPDATE [Seccion] SET [contenido] = @contenido WHERE [nombre] = @nombre;
  cmd.Parameters.AddWithValue("@contenido",contenido);
  cmd.Parameters.AddWithValue("@nombre", nombre);



运行此命令时,参数@contenido@nombre将替换为您在最后两行中分配的值.



When you run this, the parameters @contenido and @nombre will be replaced with the values you assign in the last two lines.


首先,Mark给出的答案是正确的.您应该删除对ExecuteNonQuery()的第一个调用.

数据库中nombre列的DataType是什么?

仅根据其名称来判断,它就有可能是数字.如果是,请尝试更改:

Firstly the answer given by Mark is correct. You should delete the first call to ExecuteNonQuery().

What is the DataType of the nombre column in your database?

Judging purely by its name, there is a chance that it is numeric. If it is, then try changing:

cmd.Parameters.AddWithValue("@nombre", nombre);



至:



to:

cmd.Parameters.Add("@nombre", OleDBType.Integer);
cmd.Parameters["@nombre"].Value = nombre; <=== NOTE nombre should NOT be a string.


这篇关于从文本框中更新Access数据库时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆