部分代码:
string sql = string.Format("select * from S where (SNo = '{0}' and SN = '{1}' and SAge = '{2}' and SDept = '{3}' and SClass = '{4}' and SAddress = '{5}')",
username.Text, stname.Text, stage.Text, comboBox2.Text, comboBox3.Text, stadd.Text);
SqlCommand command1 = new SqlCommand(sql, dbhelp.hong);
try
{
dbhelp.hong.Open();
int i = (int) command1.ExecuteScalar();
if (i >= 1)
{
if (username.Text != "")
{
string sql1 = string.Format("select * from S where SNo ='{0}'", username.Text);
sda = new SqlDataAdapter(sql1, dbhelp.hong);
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
sda.Fill(ds,"S");
dataGridView1.DataSource = ds.Tables["S"];
SqlCommand command = new SqlCommand(sql1, dbhelp.hong);
dbhelp.hong.Open();
SqlDataReader datareader = command.ExecuteReader();
if (!datareader.HasRows)
{
MessageBox.Show("数据库中不包含此数据,请核对后重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
dbhelp.hong.Close();}
else if (stname.Text != "")
{
string sql2 = string.Format("select * from S where SN like '%{0}%'", stname.Text);
sda = new SqlDataAdapter(sql2, dbhelp.hong);
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
sda.Fill(ds, "S");
dataGridView1.DataSource = ds.Tables["S"];
SqlCommand command = new SqlCommand(sql2, dbhelp.hong);
dbhelp.hong.Open();
SqlDataReader datareader = command.ExecuteReader();
if (!datareader.HasRows)
{
MessageBox.Show("数据库中不包含此数据,请核对后重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
dbhelp.hong.Close();
}
}
............
else
{
MessageBox.Show("您查找的用户数据与数据库不相符,请核对后重新输入!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
username.Text = "";
stname.Text = "";
}
}
catch (Exception ex)
{
MessageBox.Show("数据库出错!");
MessageBox.Show(ex.Message);
}
finally
{
dbhelp.hong.Close();
}
运行之后跳出未将对象引用设置到对象的实例
是哪里出了问题吗?
string sql = string.Format("select * from S where (SNo = '{0}' and SN = '{1}' and SAge = '{2}' and SDept = '{3}' and SClass = '{4}' and SAddress = '{5}')",
username.Text, stname.Text, stage.Text, comboBox2.Text, comboBox3.Text, stadd.Text);
SqlCommand command1 = new SqlCommand(sql, dbhelp.hong);
try
{
dbhelp.hong.Open();
int i = (int) command1.ExecuteScalar();
if (i >= 1)
{
if (username.Text != "")
{
string sql1 = string.Format("select * from S where SNo ='{0}'", username.Text);
sda = new SqlDataAdapter(sql1, dbhelp.hong);
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
sda.Fill(ds,"S");
dataGridView1.DataSource = ds.Tables["S"];
SqlCommand command = new SqlCommand(sql1, dbhelp.hong);
dbhelp.hong.Open();
SqlDataReader datareader = command.ExecuteReader();
if (!datareader.HasRows)
{
MessageBox.Show("数据库中不包含此数据,请核对后重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
dbhelp.hong.Close();}
else if (stname.Text != "")
{
string sql2 = string.Format("select * from S where SN like '%{0}%'", stname.Text);
sda = new SqlDataAdapter(sql2, dbhelp.hong);
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
sda.Fill(ds, "S");
dataGridView1.DataSource = ds.Tables["S"];
SqlCommand command = new SqlCommand(sql2, dbhelp.hong);
dbhelp.hong.Open();
SqlDataReader datareader = command.ExecuteReader();
if (!datareader.HasRows)
{
MessageBox.Show("数据库中不包含此数据,请核对后重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
dbhelp.hong.Close();
}
}
............
else
{
MessageBox.Show("您查找的用户数据与数据库不相符,请核对后重新输入!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
username.Text = "";
stname.Text = "";
}
}
catch (Exception ex)
{
MessageBox.Show("数据库出错!");
MessageBox.Show(ex.Message);
}
finally
{
dbhelp.hong.Close();
}
运行之后跳出未将对象引用设置到对象的实例
是哪里出了问题吗?
在 int i = (int) command1.ExecuteScalar();
这句结束后直接跳到
catch (Exception ex)
{
MessageBox.Show("数据库出错!");
MessageBox.Show(ex.Message);
}
command1.ExecuteScalar()这个返回的是一个object,估计应该是返回null了,然后被你强制(int)
你可以改成:
int i = (int)(command1.ExecuteScalar()?? 0);
或者用
if(int.TryParse(Convert.ToString(command1.ExecuteScalar(), out i))
{
if(i > 1)
...
}
else
....