如何删除未初始化的错误连接属性? [英] how to remove error connection property has not been initialized?

查看:93
本文介绍了如何删除未初始化的错误连接属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我消除此错误..m发送代码
这是我的数据库连接类

kindly help me in removal of this error.. m sending code
this is my class of database connection

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

namespace sqldb

{
    class DBconnection
    {
        public SqlConnection con = null;
        public SqlCommand cmd = null;
        public bool connection = true;
       
        public bool DBConnect()
        {
            try
            {
                SqlConnection con = new SqlConnection( " Data Source=AHSAN-PC\\SQLEXPRESS;Initial Catalog=user;Integrated Security=SSPI;");
                con.Open();
                MessageBox.Show("connected");
                cmd = new SqlCommand();
             


            }
            catch (Exception ex)
            {
                MessageBox.Show("database open mein connection error" + ex.Message);

            }
            return connection;
        }
        public bool DBClose()
        {
            try
            {
                if (connection == true)
                {
                    con.Close();
                    connection = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("database close connection error" + ex.Message);

            }
            return connection;
        }
    }
}


这是我的插入代码代码


this is my code for insert code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace sqldb
{
    public partial class Form1 : Form
    {
        DBconnection db = new DBconnection();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            db.DBConnect(); 

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string str="";
                str="insert into login values('" + textBox1.Text + "','" + textBox2.Text + "')";
                db.cmd.Connection = db.con;
                db.cmd.CommandText = str;
                int x;
               
                x=db.cmd.ExecuteNonQuery();
                if (x > 0)
                {
                    db.DBClose();
                    textBox1.Clear();
                    textBox2.Clear();
                    MessageBox.Show("data added successfully");
                }
                else
                {
                    MessageBox.Show("data not added successfully");
                }


                
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("error in databse query" + ex.Message);
            }

        }

       
    }
}


请帮助我消除此错误.... m使用SQL Server 2005和Visual Studio 2010,并且我的处理器是64位.....此错误是由于64位处理器问题引起的吗?请帮助我


Please help me in removal of this error....m using sql server 2005 and visual studio 2010 and my processor is 64 bit.....is this error is due to 64 bit processor problem? Please help me

推荐答案

是的,正如您在此处看到的
Yes, as you can see here that
private void Form1_Load(object sender, EventArgs e)
       {
           db.DBConnect();

       }



返回布尔值true,而不是连接.

在这一行中



returns bool value true, not the connection.

and in this line

db.cmd.Connection = db.con;

db是Dbconnection类的对象,并且db.con引用的public SqlConnection con = null;在这里设置为null,因此连接属性未初始化异常即将到来.

用此代码替换您的代码(在我的终端运行正常):

db is the object of Dbconnection class and db.con is referring to public SqlConnection con = null; which is set to null here, therefore the connection property is not initialized exception is coming.

Replace your code with this one(which is running fine at my end):

class DBconnection
    {
        public SqlConnection con = null;
        public SqlCommand cmd = null;
        public bool connection = true;

        public SqlConnection DBConnect()
        {
            try
            {

                con = new SqlConnection(" Data Source=AHSAN-PC\\SQLEXPRESS;Initial Catalog=user;Integrated Security=SSPI;");
                con.Open();
                MessageBox.Show("connected");

                //cmd = new SqlCommand();



            }
            catch (Exception ex)
            {
                MessageBox.Show("database open mein connection error" + ex.Message);

            }
            //return connection;
            return con;
        }
        public bool DBClose()
        {
            try
            {
                if (connection == true)
                {

                    con.Close();
                    connection = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("database close connection error" + ex.Message);

            }
            return connection;
        }
    }



并在插入代码中执行以下操作:



and in insertion code do this:

private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string str="";
                str="insert into login values('" + textBox1.Text + "','" + textBox2.Text + "')";
                //db.cmd.Connection = db.con;
                db.cmd = new SqlCommand();
                db.cmd.Connection = db.DBConnect();
                db.cmd.CommandText = str;
                int x;

                x=db.cmd.ExecuteNonQuery();
                if (x > 0)
                {
                    db.DBClose();
                    textBox1.Clear();
                    textBox2.Clear();
                    MessageBox.Show("data added successfully");
                }
                else
                {
                    MessageBox.Show("data not added successfully");
                }




            }
            catch (Exception ex)
            {
                MessageBox.Show("error in databse query" + ex.Message);
            }

        }



希望对您有所帮助:)

有关更多查询,请在此处评论!



hope it helps :)

for further queries comment here!


此错误是由于64位处理器问题引起的吗?
为什么不只是为x86编译并找出答案?

请帮助我清除此错误
您必须告诉我们错误"是什么,错误/意外的行为?有例外吗?编译错误?.并提供错误消息或预期的行为.



您没有告诉SqlCommand使用哪个连接.
改变
is this error is due to 64 bit processor problem?
Why don''t you just compile for x86 and find out?

Please help me in removal of this error
You have to tell us what the "error" is, wrong/unexpected behavior? an exception? a compilation error?.. and supply the error message or what behavior was expected.



You''re not telling your SqlCommand which connection to use.
change
cmd = new SqlCommand();




to

cmd = new SqlCommand(string.Empty, con);


您已经在button1_Click


You''re already doing this in button1_Click


中执行了此操作,这是我的大胆猜测,但可能会帮助您:尝试在用户之间重命名表名,并同样更改连接字符串.


希望对您有所帮助:)
Its my wild guess but might help you: try renaming your table name from user to users, And equally change your connection string.


hope it helps :)


这篇关于如何删除未初始化的错误连接属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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