sql代码问题
using System;using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public string CustomerID= "";
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("data source=ZCX;" +
"initial catalog=NorthWind;" +
"integrated security=SSPI;" +
"persist security info=False;" +
"workstation ID=ZCX;" +
"packet size=4096");
string sqlStatement = "SELECT * FROM Orders WHERE CustersID=@ID"+" ORDER BY OrderID";
SqlCommand sqlcmd = new SqlCommand(sqlStatement, conn);
sqlcmd.Parameters.AddWithValue("@ID", CustomerID);
conn.Open();
ListView lv = new ListView();
lv.BeginUpdate();
SqlDataReader sdr = sqlcmd.ExecuteReader();//运行到这里就说CustomerID无效,为什么呢?
int cols = sdr.FieldCount;
for (int i = 0; i < cols; i++)
{
lv.Columns.Add(sdr.GetName(i), 100, HorizontalAlignment.Left);
}
string[] lvitem = new string[cols];
object[] values = new object[cols];
while (sdr.Read())
{
sdr.GetValues(values);
for (int i = 0; i < values.Length; i++)
lvitem[i] = values[i].ToString();
ListViewItem lvi = new ListViewItem(lvitem);
lv.Items.Add(lvi);
}
lv.EndUpdate();
sdr.Close();
conn.Close();
}
}
}
希望大虾们帮助帮助我~~~小弟不胜感激