如何获取数据集值

时间:2010-02-16 12:46:15

标签: sql sql-server vb.net

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代码帮助

4 个答案:

答案 0 :(得分: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);
祝你好运......

答案 1 :(得分:0)

您可以使用Microsoft Enterprise Library轻松完成此过程。该库是一个用于处理数据集的强大库。

答案 2 :(得分:0)

   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;
        }

答案 3 :(得分:-1)

我认为你在寻找的是

cmd =新的SqlCommand(“Select * from'”&amp; ds.Tables(0)。 TableName &amp;“'”,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)