vs查询想判断当输入的值在SQL数据库中找不到的时候显示数据不存在,但是不知道怎么编写代码 50
privatevoidbutton1_Click(objectsender,EventArgse){if(textBox1.Text.Trim().Length==0){...
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim().Length == 0)
{
MessageBox.Show("订单号不能为空");
textBox1.Focus();
return;
}
else
{
string myConnStr = "data source=127.0.0.1;database=sales;user id=xsgl;password=123456";
SqlConnection myConn = new SqlConnection(myConnStr);
string a = "select OrderID from OrderDetail where OrderID='" + this.textBox1.Text.Trim() + "'";
SqlCommand myComm1 = new SqlCommand(a, myConn);
myConn.Open();
SqlDataReader myReader = myComm1.ExecuteReader();
if (myReader.Read())
{
// 设置要执行的SQL命令
myReader.Close();
SqlConnection myConn = new SqlConnection(myConnStr);
string sql = "select * from OrderDetail where OrderID like '%" + this.textBox1.Text.Trim() + "%'";
SqlCommand myComm = new SqlCommand(sql, myConn);
// myConn.Open();
SqlDataReader myReader = myComm.ExecuteReader();
//设置返回查询结果的表变量
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("OrderID", typeof(string)));
dt.Columns.Add(new DataColumn("ProductID", typeof(string)));
dt.Columns.Add(new DataColumn("Quantity", typeof(string)));
dt.Columns.Add(new DataColumn("TotalPrice", typeof(string)));
dt.Columns.Add(new DataColumn("CustomerID", typeof(string)));
//读查询的结果并放入表中
while (myReader.Read())
{
textBox1.Text = myReader.GetValue(0).ToString();
textBox2.Text = myReader.GetValue(1).ToString();
textBox3.Text = myReader.GetValue(2).ToString();
textBox4.Text = myReader.GetValue(3).ToString();
textBox5.Text = myReader.GetValue(4).ToString();
}
//关闭数据库连接
myConn.Close();
}
else
{
MessageBox.Show("无此数据");
myConn.Close();
}
}
}
这是我单击查询按钮的代码,可是提示我索引数组超出数组界限 展开
{
if (textBox1.Text.Trim().Length == 0)
{
MessageBox.Show("订单号不能为空");
textBox1.Focus();
return;
}
else
{
string myConnStr = "data source=127.0.0.1;database=sales;user id=xsgl;password=123456";
SqlConnection myConn = new SqlConnection(myConnStr);
string a = "select OrderID from OrderDetail where OrderID='" + this.textBox1.Text.Trim() + "'";
SqlCommand myComm1 = new SqlCommand(a, myConn);
myConn.Open();
SqlDataReader myReader = myComm1.ExecuteReader();
if (myReader.Read())
{
// 设置要执行的SQL命令
myReader.Close();
SqlConnection myConn = new SqlConnection(myConnStr);
string sql = "select * from OrderDetail where OrderID like '%" + this.textBox1.Text.Trim() + "%'";
SqlCommand myComm = new SqlCommand(sql, myConn);
// myConn.Open();
SqlDataReader myReader = myComm.ExecuteReader();
//设置返回查询结果的表变量
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("OrderID", typeof(string)));
dt.Columns.Add(new DataColumn("ProductID", typeof(string)));
dt.Columns.Add(new DataColumn("Quantity", typeof(string)));
dt.Columns.Add(new DataColumn("TotalPrice", typeof(string)));
dt.Columns.Add(new DataColumn("CustomerID", typeof(string)));
//读查询的结果并放入表中
while (myReader.Read())
{
textBox1.Text = myReader.GetValue(0).ToString();
textBox2.Text = myReader.GetValue(1).ToString();
textBox3.Text = myReader.GetValue(2).ToString();
textBox4.Text = myReader.GetValue(3).ToString();
textBox5.Text = myReader.GetValue(4).ToString();
}
//关闭数据库连接
myConn.Close();
}
else
{
MessageBox.Show("无此数据");
myConn.Close();
}
}
}
这是我单击查询按钮的代码,可是提示我索引数组超出数组界限 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询