注册 登录
编程论坛 SQL Server论坛

sql代码求助

anixilu 发布于 2008-12-06 11:10, 920 次点击
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();
            }

        }
    }
求求各位大虾帮帮忙吧 帮我看看是为什么~~小弟不胜感激
4 回复
#2
康elon2008-12-06 18:16
看不懂...太茫然了。
#3
球球2008-12-08 13:18
首先看下"SELECT * FROM Orders WHERE CustersID=@ID ORDER BY OrderID";中的CustersID是否正确
再把conn.Open();放在sqlcmd定义之前。

[[it] 本帖最后由 球球 于 2008-12-8 13:25 编辑 [/it]]
#4
2008-12-08 14:51
你的“CustomerID“这个参数,一开始定义了为空,查询的时候用的也是空值,放在SQL里面就相当于没有输入字符串的结果,你可以单步看看,到了这一步,是不是CustomerID的值是空的。
#5
球球2008-12-08 21:18
他定义的是public类型,可能在之前有个赋值的过程,
1