背景:
  数据表一(Task):
  TaskID  (Key)
    TaskTypeID
    ...
        数据表二(TaskType):
    TaskTypeID(Key)
    TaskTypeName (Key)
    ...两个表通过TaskTypeID关联起来。然后在form中使用一个dataGrid,用来显示Task表内容;
同时提供一个comboBox来显示TaskTypeName,以方便根据TaskTypeName来在dataGrid中显示相关Task表中的内容。因此在创建select 语句的时候,改成 select * from Task Where (TaskTypeID LIKE ?)问题:
1、为了在form_load的时候显示所有内容,则加入如下语句
   sqlDaTask.SelectCommand.Parameters[0].Value ="%";
   结果总是报错:输入字符串格式不正确
   加入如下一条可以解决:
   sqlDaTask.SelectCommand.Parameters[0].SqlDbType = SqlDbType.VarChar;
   还有没有别的方法可以解决这个问题?2、启动的时候检索TaskType表,将其中的TaskTypeName通过comboBox.Items.Add方法加入到comboBox,则在查询的时候需要将sqlDaTask.SelectCommand.Parameters[0].Value设成何值?如果是通过selectedIndex,则TaskType表中的TaskTypeID不连续怎么办?

解决方案 »

  1.   

    select TaskTypeId,TaskTypeName from TaskType 循环取出 TaskTypeId,TaskTypeName 的所有值分别创建 ItemVO loItem = new ItemVO("TaskTypeId","TaskTypeName ")comboBox1.Items.Add(loItem);
    取值:
      ((ItemVO)comboBox1.SelectedItem).id ----这个是你的TaskTypeId 的值。
      ((ItemVO)comboBox1.SelectedItem).desc------这个是 TaskTypeName 的值,comboBox显示这个值===================================================
    public class ItemVO : System.Object
    {
    public string id;
    public string desc;

    public ItemVO(string Id, string Desc)
    {
    this.id = Id;
    this.desc = Desc;
    } public override string ToString() 
    {
    return this.desc;
    } public override bool Equals(System.Object obj) 
    {
    if (this.GetType().Equals(obj.GetType())) 
    {
    ItemVO that=(ItemVO) obj;
    return (this.id.Equals(that.id));
    }
    return false;
    }
    public override int GetHashCode()
    {
    return this.id.GetHashCode();;
    }
    }================
      

  2.   

    to:
    这样的确能够解决第二个问题中的TaskTypeID不连续的问题,但还是不能使我的查询生效啊
    另外:TaskTypeID在sql server中定义为smallint
      

  3.   

    直接将表2绑定到combobx,TaskTypeID  为 valuemember, TaskTypeName  为displaymember;select * from Task Where (TaskTypeID = (select TaskTypeID   from whre  TaskTypeName  LIKE ?))  //参数为combobx中的displayMember;
      

  4.   

    selectedIndex
    是你的combo的当前选项的数值而且设定的时候
    都是连续的
    不会有断开的现象