【问题标题】:C# SqlDataAdapter not populating DataSetC# SqlDataAdapter 不填充数据集
【发布时间】:2010-04-20 17:16:12
【问题描述】:

我已经搜索了网络并搜索了网络,但并没有完全找到我可能遇到的问题。我目前在获取 SqlDataAdapter 来填充 DataSet 时遇到问题。我正在运行 Visual Studio 2008,并且查询被发送到 SqlServer 2008 的本地实例。如果我运行查询 SqlServer,它会返回结果。 代码如下:

string theQuery = "select Password from Employees where employee_ID = '@EmplID'";    
SqlDataAdapter theDataAdapter = new SqlDataAdapter();
theDataAdapter.SelectCommand = new SqlCommand(theQuery, conn);
theDataAdapter.SelectCommand.Parameters.Add("@EmplID", SqlDbType.VarChar).Value = "EmployeeName";
theDataAdapter.Fill(theSet);

读取数据集的代码:

foreach (DataRow theRow in theSet.Tables[0].Rows)
{
    //process row info
}

如果我可以提供更多信息,请告诉我。

【问题讨论】:

    标签: c# sql-server-2008 dataset sqldataadapter


    【解决方案1】:

    您需要查询“从员工中选择密码,其中employee_ID = @EmplID”(参数周围没有单引号)。

    【讨论】:

    • 就是这样,非常感谢。我会尽快接受答案。
    • 第二组眼睛就足够了。有时他们甚至不必成为聪明头脑的一部分;-)
    【解决方案2】:

    如果您运行此查询,它会返回结果吗?

    select Password from Employees where employee_ID = 'EmployeeName'
    

    我的猜测是“EmployeeName”应该是一些传入的值...... 如果您使用参数,@EmpID 在查询中不应有单引号。

    【讨论】:

      猜你喜欢
      • 2013-10-07
      • 1970-01-01
      • 2017-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-21
      相关资源
      最近更新 更多