ExecuteNonQuery: Connection 属性尚未初始化。
privatevoidbutton2_Click(objectsender,EventArgse){StringstrConn1="Provider=Microsoft....
private void button2_Click(object sender, EventArgs e)
{
String strConn1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\member.mdb";
OleDbConnection conn1 = new OleDbConnection(strConn1);
OleDbDataAdapter oleDbDataAdapter1 = new OleDbDataAdapter("select * from Users",conn1);
string userName = txtName.Text.Trim();
string sql = "select count(*)from Users where UserName='" + userName + "'";
conn1.Open();
OleDbCommand cmd = new OleDbCommand(sql, conn1);
int n = (int)cmd.ExecuteScalar();
if (n > 0)
{
MessageBox.Show("用户已存在");
return;
}
else
{
OleDbCommand cmd2 = new OleDbCommand ();
cmd2.CommandText = "insert into Users " + "(UserName,Sex,Password,RealName,Email,Question,Answer)" + "values (@UserName,@Sex@Password,@RealName,@Email,@Question,@Answer)";
cmd2.Parameters.Add("@UserName", OleDbType.Char, 6, "UserName");
cmd2.Parameters.Add("@Sex", OleDbType.Char, 2, "Sex");
cmd2.Parameters.Add("@RealName", OleDbType.Char, 6, "RealName");
cmd2.Parameters.Add("@Password", OleDbType.Char, 6, "Password");
cmd2.Parameters.Add("@Question", OleDbType.Char, 6, "Question");
cmd2.Parameters.Add("@Email", OleDbType.Char, 20, "Email");
cmd2.Parameters.Add("@Answer", OleDbType.Char, 10, "Answer");
cmd2.Parameters["@UserName"].Value = txtName.Text;
cmd2.Parameters["@Sex"].Value = checkedListBox1.Text;
cmd2.Parameters["@RealName"].Value = txtRealName.Text;
cmd2.Parameters["@Password"].Value = txtPwd1.Text;
cmd2.Parameters["@Question"].Value = comboBox1.Text;
cmd2.Parameters["@Email"].Value = txtEmail.Text;
cmd2.Parameters["@Answer"].Value = txtAnswer.Text;
cmd2.ExecuteNonQuery();
DataSet dataSet11 = new DataSet();
oleDbDataAdapter1.Fill(dataSet11, "Users");
conn1.Close();
555555555怎么改 展开
{
String strConn1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\member.mdb";
OleDbConnection conn1 = new OleDbConnection(strConn1);
OleDbDataAdapter oleDbDataAdapter1 = new OleDbDataAdapter("select * from Users",conn1);
string userName = txtName.Text.Trim();
string sql = "select count(*)from Users where UserName='" + userName + "'";
conn1.Open();
OleDbCommand cmd = new OleDbCommand(sql, conn1);
int n = (int)cmd.ExecuteScalar();
if (n > 0)
{
MessageBox.Show("用户已存在");
return;
}
else
{
OleDbCommand cmd2 = new OleDbCommand ();
cmd2.CommandText = "insert into Users " + "(UserName,Sex,Password,RealName,Email,Question,Answer)" + "values (@UserName,@Sex@Password,@RealName,@Email,@Question,@Answer)";
cmd2.Parameters.Add("@UserName", OleDbType.Char, 6, "UserName");
cmd2.Parameters.Add("@Sex", OleDbType.Char, 2, "Sex");
cmd2.Parameters.Add("@RealName", OleDbType.Char, 6, "RealName");
cmd2.Parameters.Add("@Password", OleDbType.Char, 6, "Password");
cmd2.Parameters.Add("@Question", OleDbType.Char, 6, "Question");
cmd2.Parameters.Add("@Email", OleDbType.Char, 20, "Email");
cmd2.Parameters.Add("@Answer", OleDbType.Char, 10, "Answer");
cmd2.Parameters["@UserName"].Value = txtName.Text;
cmd2.Parameters["@Sex"].Value = checkedListBox1.Text;
cmd2.Parameters["@RealName"].Value = txtRealName.Text;
cmd2.Parameters["@Password"].Value = txtPwd1.Text;
cmd2.Parameters["@Question"].Value = comboBox1.Text;
cmd2.Parameters["@Email"].Value = txtEmail.Text;
cmd2.Parameters["@Answer"].Value = txtAnswer.Text;
cmd2.ExecuteNonQuery();
DataSet dataSet11 = new DataSet();
oleDbDataAdapter1.Fill(dataSet11, "Users");
conn1.Close();
555555555怎么改 展开
展开全部
一楼正解,cmd2没有指定Connection
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
cmd2 New的时候没有给他conn参数啊
要么cmd2.connection=……;
要么就OleDbCommand cmd2=New OleDbCommand(text,conn1)
要么cmd2.connection=……;
要么就OleDbCommand cmd2=New OleDbCommand(text,conn1)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
connection
换一个变量名。
报错原因是没有初始化,就是说
没有连接上。
看一下数据库服务是否开启,数据库连接语句是否出错。
换一个变量名。
报错原因是没有初始化,就是说
没有连接上。
看一下数据库服务是否开启,数据库连接语句是否出错。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这就说明的连接代码有问题,不能正确连接到数据库。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询