连接属性尚未在C#中初始化 [英] Connection property has not been initialized in C#

查看:88
本文介绍了连接属性尚未在C#中初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新我的SQL服务器压缩数据库时发生错误(.sdf)

我正在使用C#windows窗体应用程序

这是错误



ExecuteNonQuery:连接属性尚未初始化。



这是我的更新代码

我在这里停留了3h是什么问题



提前感谢!



An error occurred when I update my SQL server compact database (.sdf)
I'm using C# windows form application
This is the error

ExecuteNonQuery: Connection property has not been initialized.

This is my update code
What is the wrong I stuck 3h in here

Thank in advance !

int index = cb1.SelectedIndex; //combo box
int uid=1;
            

            SqlCeConnection con = new SqlCeConnection("Data Source="C:\application\application.sdf;Persist Security Info=False;");
            con.Open();
            SqlCeCommand cm = new SqlCeCommand("UPDATE tbl_user SET u_name=@u_name , u_scl=@u_scl , u_ext=@u_ext WHERE u_id=@u_id");
            cm.Parameters.AddWithValue("@u_id", uid);
            cm.Parameters.AddWithValue("@u_name", txtname.ToString());
            cm.Parameters.AddWithValue("@u_scl", txtscl.ToString());
            cm.Parameters.AddWithValue("@u_ext",index );
           
            try
            {
                int eff = cm.ExecuteNonQuery();
                
                if (eff == 1)
                {
                    con.Close();
                    MessageBox.Show("User Added", "Done");
                }
                else
                {
                    con.Close();
                    MessageBox.Show("not inserted ", "Error");
                }
            }
            catch (SqlCeException ex)
            {
                MessageBox.Show(ex.ToString());
            }

推荐答案

简单。你创建了一个SqlCeConnection对象和一个SqlCeCommand对象,但你从未告诉命令对象使用连接对象。



我认为你在寻找这个 [ ^ ]。
Easy. You made a SqlCeConnection object and a SqlCeCommand object, but you never told the command object to use the connection object.

I think you're looking for this[^].


这篇关于连接属性尚未在C#中初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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