int no = 0;
try
{
string sql = string.Format("select count(*) from Users where Id={0} and LoginPwd='{1}'", Convert.ToInt32(comboBox1.Text), textBox1.Text);
SqlCommand com = new SqlCommand(sql, DBHelper.con);
DBHelper.con.Open();
no = (int)com.ExecuteScalar();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBHelper.con.Close();
}
if (no == 1)
{
int num = 0;
if (checkBox1.Checked)
{
try
{
string sql = string.Format("select StorageId from Storage where StorageId={0}", Convert.ToInt32(comboBox1.Text));
DBHelper.con.Open();
SqlCommand com = new SqlCommand(sql, DBHelper.con);
num = (int)com.ExecuteScalar();
if (num == 1)
{
sql = string.Format("Update Storage Set LoginPwd='{0}' and State='{1}' where StorageId='{2}' ", textBox1.Text, comboBox2.Text, Convert.ToInt32(comboBox1.Text));
com.CommandText = sql;
int num2 = com.ExecuteNonQuery();
if (num2 > 0)
{
MessageBox.Show("操作成功");
}
}
else
{
string cheng = (comboBox2.Text == "")? null:comboBox2.Text;
sql = string.Format("Insert into Storage(StorageId, LoginPwd, State) Values({0},'{1}','{2}')", Convert.ToInt32(comboBox1.Text), textBox1.Text,cheng);
com.CommandText = sql;
int num2 = com.ExecuteNonQuery();
if (num2 == 1)
{
MessageBox.Show("插入成功");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBHelper.con.Close();
}
}
try
{
string sql = string.Format("select count(*) from Users where Id={0} and LoginPwd='{1}'", Convert.ToInt32(comboBox1.Text), textBox1.Text);
SqlCommand com = new SqlCommand(sql, DBHelper.con);
DBHelper.con.Open();
no = (int)com.ExecuteScalar();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBHelper.con.Close();
}
if (no == 1)
{
int num = 0;
if (checkBox1.Checked)
{
try
{
string sql = string.Format("select StorageId from Storage where StorageId={0}", Convert.ToInt32(comboBox1.Text));
DBHelper.con.Open();
SqlCommand com = new SqlCommand(sql, DBHelper.con);
num = (int)com.ExecuteScalar();
if (num == 1)
{
sql = string.Format("Update Storage Set LoginPwd='{0}' and State='{1}' where StorageId='{2}' ", textBox1.Text, comboBox2.Text, Convert.ToInt32(comboBox1.Text));
com.CommandText = sql;
int num2 = com.ExecuteNonQuery();
if (num2 > 0)
{
MessageBox.Show("操作成功");
}
}
else
{
string cheng = (comboBox2.Text == "")? null:comboBox2.Text;
sql = string.Format("Insert into Storage(StorageId, LoginPwd, State) Values({0},'{1}','{2}')", Convert.ToInt32(comboBox1.Text), textBox1.Text,cheng);
com.CommandText = sql;
int num2 = com.ExecuteNonQuery();
if (num2 == 1)
{
MessageBox.Show("插入成功");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBHelper.con.Close();
}
}
运行结果到这里就错误
int num2 = com.ExecuteNonQuery(); 说是 未将对象引用到设置的对象实例 别的代码没问题 。 就出现在这了 。 我找了很多遍都没找出什么原因引起的错误
你应该再一次创建命令对象的实例,或者是说你应该把第一次创建的对象放在最外面(作为全局变量)//int num2 = com.ExecuteNonQuery();
在这已经访问不到com对象实例了
DBHelper.con.Open();
SqlCommand com = new SqlCommand(sql, DBHelper.con);
num = (int)com.ExecuteScalar();
if (num == 1)
{
sql = string.Format("Update Storage Set LoginPwd='{0}' and State='{1}' where StorageId='{2}' ", textBox1.Text, comboBox2.Text, Convert.ToInt32(comboBox1.Text)); SqlCommand com = new SqlCommand(sql, DBHelper.con); com.CommandText = sql;
int num2 = com.ExecuteNonQuery();
if (num2 > 0)
{
MessageBox.Show("操作成功");
}
{
sql = string.Format("Update Storage Set LoginPwd='{0}' and State='{1}' where StorageId='{2}' ", textBox1.Text, comboBox2.Text, Convert.ToInt32(comboBox1.Text)); SqlCommand com = new SqlCommand(sql, DBHelper.con);
com.CommandText = sql;
int num2 = com.ExecuteNonQuery();
if (num2 > 0)
{
MessageBox.Show("操作成功");
}