数据适配器更新问题 [英] Dataadapter update problem

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

问题描述



我是csharp的新手,我无法更新dataadapter,这是代码

 命名空间 app1
{
    公共 部分  class  Form1:表单
    {
        MySql.Data.MySqlClient.MySqlConnection conn;
     
       公共 Form1()
        {
            InitializeComponent();
        }

        私有 无效 Form1_Load(对象发​​件人,EventArgs e)
        {}

        私有 无效 radPageViewPage1_Paint(对象发​​件人,PaintEventArgs e)
        {
        }

        公共 无效 radButton2_Click(对象发​​件人,EventArgs e)
        {

            字符串 myConnectionString;
          
            myConnectionString = "  +
                " ;
            
            conn =  MySql.Data.MySqlClient.MySqlConnection(myConnectionString);

                 conn.Open();

            DataTable dtusers =  DataTable();
             dtusers = Getusers();
             radGridView2.DataSource = dtusers;
               conn.Close();
             
        }



       私有 DataTable Getusers()
        {

        字符串查询= " ;
        MySqlDataAdapter adapter =  MySqlDataAdapter(query,conn); // 适配器声明
            DataSet ds =  DataSet();
            adapter.Fill(ds);
            
            adapter.UpdateCommand =  MySqlCommand(
            " ,conn);
            adapter.UpdateCommand.Parameters.Add(" ,MySqlDbType.VarChar,"   userrid");
            adapter.UpdateCommand.Parameters.Add(" ,MySqlDbType.VarChar,"  密码");
            adapter.UpdateCommand.Parameters.Add(" ,MySqlDbType.VarChar,"   user_group");
            adapter.UpdateCommand.Parameters.Add(" ,MySqlDbType.VarChar,"  状态");
            adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.None;

            返回 ds.Tables [ 0 ];

                      

        }


       公共 无效 save()
       {
          
       }

        私有 无效 button1_Click(对象发​​件人,EventArgs e)
        {
         
        }

        私有 无效 radButton1_Click(对象发​​件人,EventArgs e)
        {

            adapter.update();

            // 名称适配器在当前上下文中不存在
        }



    }
} 




我能够检索数据.
但是当我想在其他按钮事件中添加命令adapter.update()时,出现此错误,称为

名称适配器在当前上下文中不存在.


检查了很多示例,但似乎没人能得到当前的上下文错误.

在此先感谢
Omer

解决方案

适配器是在方法Getusers中定义的,因此其他方法不可见.考虑将适配器与conn一起定义为类变量.像这样的东西:

 公共 部分  class  Form1:表格
    {
        MySql.Data.MySqlClient.MySqlConnection conn;
        MySqlDataAdapter适配器;
...
私有 DataTable Getusers()
        {
 
        字符串查询= " ;
        adapter =  MySqlDataAdapter(query,conn);
... 


另请阅读有关范围的更多信息,例如: http://www.blackwasp.co.uk/CSharpVariableScopes.aspx [ ^ ]


Hi,

I am new to csharp, I cannot update the dataadapter , Here is the code

namespace app1
{
    public partial class Form1 : Form
    {
        MySql.Data.MySqlClient.MySqlConnection conn;
     
       public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {     }

        private void radPageViewPage1_Paint(object sender, PaintEventArgs e)
        {
        }

        public void radButton2_Click(object sender, EventArgs e)
        {

            string myConnectionString;
          
            myConnectionString = "server=localhost;uid=root;" +
                "pwd=xxxx;database=ldis;";
            
            conn = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString);

                 conn.Open();

            DataTable dtusers = new DataTable() ;
             dtusers = Getusers();
             radGridView2.DataSource = dtusers;
               conn.Close();
             
        }



       private DataTable Getusers()
        {

        string query = "select userid,password,user_group,status,expiry from users";
        MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn); // adapter declaration
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            
            adapter.UpdateCommand = new MySqlCommand(
            "UPDATE users SET  password=@password, user_group=@user_group,status=@status  WHERE userid=@userid;", conn);
            adapter.UpdateCommand.Parameters.Add("@userid", MySqlDbType.VarChar, 25, "userrid");
            adapter.UpdateCommand.Parameters.Add("@password", MySqlDbType.VarChar, 45, "password");
            adapter.UpdateCommand.Parameters.Add("@user_group", MySqlDbType.VarChar, 45, "user_group");
            adapter.UpdateCommand.Parameters.Add("@status", MySqlDbType.VarChar, 10, "status");      
            adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.None;

            return ds.Tables[0];

                      

        }


       public void save()
       {
          
       }

        private void button1_Click(object sender, EventArgs e)
        {
         
        }

        private void radButton1_Click(object sender, EventArgs e)
        {

            adapter.update();

            // The name adapter does not exists in the current context
        }



    }
}




I am able to retrieve the data.
but when I want to add the command adapter.update() in other buttons event, I get this error called ,

The name adapter does not exists in current context.


Checked lots of examples, but no one seems to get this current context errors.

Thanks in advance
Omer

解决方案

The adapter is defined in your method Getusers so it''s not visible to other methods. Consider defining the adapter as class variable along with the conn. Something like:

public partial class Form1 : Form
    {
        MySql.Data.MySqlClient.MySqlConnection conn;
        MySqlDataAdapter adapter;
...
private DataTable Getusers()
        {
 
        string query = "select userid,password,user_group,status,expiry from users";
        adapter = new MySqlDataAdapter(query, conn); 
...


Also read more about scopes, for example: http://www.blackwasp.co.uk/CSharpVariableScopes.aspx[^]


这篇关于数据适配器更新问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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