C#刷新访问数据库 [英] C# refresh access database

查看:121
本文介绍了C#刷新访问数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am able to Save Data to a Access databse , now when i add new entry i try to refresh the database but the new entry dont show until i close the program and open it back up. I have 2 buttons one Update and the other Refresh. I am using Parameters. It would be better to use one meathod to do both (thats over my head at the moment) so i have 2 buttons. I dont get any errors when i try to Refresh so I dont know where to start.
 
Thanks 
 
My Update code working fine.

<pre lang="c#"><pre>private void btnUpdate_Clicked(object sender, RoutedEventArgs e)  
       {  
           using (var myCon = new OleDbConnection(ConfigurationManager.ConnectionStrings["Connection"].ToString()))  
           {  
               using (var myCmd = new OleDbCommand("insert into [Sheet1](Box1,Box2,Box3,Box4,Box5,Box6,Box7,Box8)Values(@nm1,@nm2,@nm3,@nm4,@nm5,@nm6,@nm7,@nm8)", myCon))  
               {                      
                   myCmd.Parameters.AddWithValue("@nm1", txt_Box1.Text);  
                   myCmd.Parameters.AddWithValue("@nm2", txt_Box2.Text);  
                   myCmd.Parameters.AddWithValue("@nm3", txt_Box3.Text);  
                   myCmd.Parameters.AddWithValue("@nm4", txt_Box4.Text);  
                   myCmd.Parameters.AddWithValue("@nm5", TXT_Box5.Text);  
                   myCmd.Parameters.AddWithValue("@nm6", txt_Box6.Text);  
                   myCmd.Parameters.AddWithValue("@nm7", txt_Box7.Text);  
                   myCmd.Parameters.AddWithValue("@nm8", txt_Box8.Text);  
  
                   try  
                   {  
                       myCon.Open();  
                       int z = myCmd.ExecuteNonQuery();  
                       if (z > 0)  
                       {  
                           MessageBox.Show("Data Inserted");  
  
                      }  
  
                   }  
                   catch (Exception ex)  
                   {  
                        MessageBox.Show(ex.Message);
                        return;
                   }  
               }  
           }  
  
              
       }  





我的刷新没有刷新数据库,没有错误给出。





My Refresh is not refreshing the data base no errors givens.

private void btn_Refresh_Click(object sender, EventArgs e)  
        {  
  
            using (var myCon = new OleDbConnection(ConfigurationManager.ConnectionStrings["Connection"].ToString()))  
            {  
                using (var myCmd = new OleDbCommand("UPDATE Sheet1 SET Box1 =@nm1,Box2 = @nm2, Box3=@nm3, Box4 = @nm4, Box5 = @nm5,Box6  = @nm6, Box7 = @nm7 WHERE Box8 = @nm8", myCon))  
                {                      
                    myCmd.Parameters.AddWithValue("@nm1", txt_Box1.Text);  
                    myCmd.Parameters.AddWithValue("@nm2", txt_Box2.Text);  
                    myCmd.Parameters.AddWithValue("@nm3", txt_Box3.Text);  
                    myCmd.Parameters.AddWithValue("@nm4", txt_Box4.Text);  
                    myCmd.Parameters.AddWithValue("@nm5", TXT_Box5.Text);  
                    myCmd.Parameters.AddWithValue("@nm6", txt_Box6.Text);  
                    myCmd.Parameters.AddWithValue("@nm7", txt_Box7.Text);  
                    myCmd.Parameters.AddWithValue("@nm8", txt_Box8.Text);  
  
                    try  
                    {  
                        myCon.Open();  
                        int z = myCmd.ExecuteNonQuery();  
                        MessageBox.Show("Data Updated");  
                         
                    }  
                    catch (Exception ex)  
                    {  
                        //Handle exception as needed  
                    }  
                }  
            }  
  
             
        }  





获取数据





Get Data

private void loadgrid()
      {
          OleDbConnection con = new OleDbConnection();
          con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
          con.Open();
          OleDbCommand cmd = new OleDbCommand();
          cmd.CommandText = "Select * from [Sheet1]";
          cmd.Connection = con;
          OleDbDataReader rd = cmd.ExecuteReader();
          dataGrid.ItemsSource = rd;

      }





我的尝试:



在互联网和示例程序上查看的时间



What I have tried:

Hours of looking on the internet and sample programs

推荐答案

简单:您的INSERT不起作用,你的 catch 块会丢弃这些信息而忽略它。

为什么?

这个:

Simple: your INSERT doesn't work, and your catch block discards the information and ignores it.
Why?
This:
myCmd.Parameters.AddWithValue("@nm1", txt_Box1);  
myCmd.Parameters.AddWithValue("@nm2", txt_Box2);  

应该是:

myCmd.Parameters.AddWithValue("@nm1", txt_Box1.Text);  
myCmd.Parameters.AddWithValue("@nm2", txt_Box2.Text);  





不要留空 catch 块 - 它们隐藏错误而不是让出错你知道需要修复什么...



Don't leave blank catch blocks - they hide errors instead of letting you know what needs fixing...


不确定我是否正确解释了这个问题,但是在btnUpdate_Clicked事件中插入了数据,在btn_Refresh_Click中你更新了数据。



您从哪里获取新插入的数据或将数据从数据库更新到程序中?



I可以想象你有一个更新/保存按钮,根据它是现有的还是新的更新或插入记录,刷新按钮会执行SELECT语句以从数据库中获取数据。由于这两个按钮现在都没有获取数据,我猜你只在程序打开时才进行提取,这就是为什么在保存后你没有在程序中看到新数据的原因。


另外,如前所述,确实正确处理异常,至少向用户显示消息文本。
Not sure if I interpret the question correctly, but in the btnUpdate_Clicked event you insert the data and in btn_Refresh_Click you update the data.

Where do you fetch the newly inserted or update data from the database into the program?

I would imagine that you have an update/save button which either updates or inserts the record depending if it's an existing one or new and the the refresh button would do a SELECT statement in order to fetch the data from the database. Since neither of the buttons now fetch the data I guess that you only do the fetch when the program open and this is why you don't see the new data in your program after saving.

Also, as already mentioned do handle exceptions properly, at minimum show the message text to the user.


这篇关于C#刷新访问数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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