【问题标题】:How to get a dataset value如何获取数据集值
【发布时间】:2010-02-16 12:46:14
【问题描述】:

VB.Net 新手,

如何插入或选择数据集值。

    cmd = New SqlCommand("Select * from table1", con)
    ada = New SqlDataAdapter(cmd)
    ds = New DataSet
    ada.Fill(ds)
    cmd = New SqlCommand("Select * from  '" & ds.Tables(0) & "'  ", con)
    mydatatable1.Load(dr3)

'" & ds.Tables(0) & "'显示错误,我要获取数据集值

需要 VB.Net 代码帮助

【问题讨论】:

  • @Gopal - 您是否尝试将 ds.Tables(0)(第一个 select 语句中的数据表)与 mydatatable1 合并?我不知道变量 dr3 是什么...
  • @thedugas - 我认为它比这更基本。我认为他只是想要来自 table1 的 DataTable 格式的数据,但没有意识到 Fill 已经做到了。
  • @Mark Brittingham - 明白了...
  • Gopal - 感谢您选择我的答案作为“答案”。

标签: sql sql-server vb.net


【解决方案1】:

在您尝试创建第二个 SqlCommand 之前,您有一个合理的想法。也就是说,一旦你完成了填充,你就已经在表格中拥有了数据。你不会运行另一个选择 - 你已经这样做了。您只需引用要在数据集中使用的表。

如果你想要一个数据表,你可以这样做(对于 VB,见下文):

    SqlDataAdapter myAdapter = new SqlDataAdapter(CommandText, con);
    DataSet myDataset = new DataSet();
    myAdapter.Fill(myDataset, "Table");  // "Table" is just a name, it can be anything.
    mydatatable1 = myDataset.Tables[0];  // Get the table

现在,如果您本身不需要 DataTable,而只想从查询中读取数据,则可以使用 SqlDataReader:

 cmd = new SqlCommand(CommandText, con);
 // One or more params
 cmd.Parameters.AddWithValue("@paramName", Value);
 SqlDataReader nwReader = cmd.ExecuteReader();

然后从 nwReader 中读取:

 while (nwReader.Read())
 {
     string field1Val = (string)nwReader["FieldName"];
     etc...
 }

更新:我不了解 VB,但这是我认为的样子:

cmd = New SqlCommand("Select * from table1", con) 
ada = New SqlDataAdapter(cmd) 
ds = New DataSet 
ada.Fill(ds) 
mydatatable1 = ds.Tables(0);

您可能可以缩短它以摆脱额外的 SqlCommand(假设 VB 支持这种 SqlDataAdapater 语法,如 C#。

ada = New SqlDataAdapter("Select * from table1", con) 
ds = New DataSet 
ada.Fill(ds) 
mydatatable1 = ds.Tables(0);

祝你好运……

【讨论】:

    【解决方案2】:

    您可以使用 Microsoft Enterprise Library 来简化此过程。这个库是处理数据集的强大库。

    【讨论】:

      【解决方案3】:
         public async Task<ResponseStatusViewModel> GetAll()
              {
                  var responseStatusViewModel = new ResponseStatusViewModel();
                  var connection = new SqlConnection(EmployeeConfig.EmployeeConnectionString);
                  var command = new SqlCommand("usp_GetAllEmployee", connection);
                  command.CommandType = CommandType.StoredProcedure;
      
                  try
                  {
                      await connection.OpenAsync();
                      var reader = await command.ExecuteReaderAsync();
      
                      var dataSet = new DataSet();
                      dataSet.Load(reader, LoadOption.OverwriteChanges, new string[] { "Employee" });
                      reader.Close();
                      reader.Dispose();
      
                      var employees = new List<EmployeeModel>();
      
                      if (dataSet.Tables.Count > 0 && dataSet.Tables["Employee"] != null)
                      {
                          var employeeTable = dataSet.Tables["Employee"];
      
                          for (int i = 0; i < employeeTable.Rows.Count; i++)
                          {
                              var employeeRow = employeeTable.Rows[i];
      
                              employees.Add(new EmployeeModel
                              {
                                  EmployeeID = Convert.ToInt32(employeeRow["EmployeeID"]),
                                  EmployeeName = Convert.ToString(employeeRow["EmployeeName"]),
                                  Address = Convert.ToString(employeeRow["Address"]),
                                  GrossSalary = Convert.ToDouble(employeeRow["GrossSalary"]),
                                  PF = Convert.ToDouble(employeeRow["PF"]),
                                  TotalSalary = Convert.ToDouble(employeeRow["TotalSalary"])
                              });
                          }
                      }
                      responseStatusViewModel.Data = employees;
                      command.Dispose();
                      connection.Dispose();
      
                  }
                  catch (Exception ex)
                  {
      
                      throw ex;
                  }
      
                  return responseStatusViewModel;
              }
      

      【讨论】:

        【解决方案4】:

        我认为你正在寻找的是

        cmd = New SqlCommand("Select * from '" & ds.Tables(0).TableName & "'", con)

        cmd = New SqlCommand("Select * from table1", con)
        ada = New SqlDataAdapter(cmd)
        ds = New DataSet
        ada.Fill(ds)
        cmd = New SqlCommand("Select * from  '" & ds.Tables(0).TableName & "'  ", con)
        mydatatable1.Load(dr3)
        

        【讨论】:

        • -1 - 你为什么要这样做?第二个 cmd 只是第一个的重新运行,没有任何功能。此外,即使有 一些防御措施,您也有一组额外的单引号。
        • @Sachin。我用你的查询,它显示错误为“表附近的语法不正确”。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-22
        • 1970-01-01
        • 1970-01-01
        • 2015-09-06
        • 2014-02-26
        相关资源
        最近更新 更多