问题描述
请帮助我消除此错误..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
推荐答案
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!
cmd = new SqlCommand();
到
to
cmd = new SqlCommand(string.Empty, con);
您已经在button1_Click
You''re already doing this in button1_Click
这篇关于如何删除未初始化的错误连接属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!