Sql 如何获取数据集值

Sql 如何获取数据集值,sql,sql-server,vb.net,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.Loa

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.table0&”中显示错误,我想获取数据集值


需要VB.Net代码帮助

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

如果您想要一个数据表,您可以对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);
假设VB支持类似于C的SqlDataAdapater语法,那么您可能可以缩短此时间以摆脱额外的SqlCommand

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

祝您好运…

您可以使用Microsoft企业库简化此过程。这个库是一个强大的用于处理数据集的库。

我想您需要的是

cmd=New-SqlCommandSelect*from'&ds.table0.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)

@Gopal-是否尝试将ds.TABLE0从第一个select语句中的datatable与mydatatable1合并?我不知道变量dr3是什么…@thedugas-我认为它比这个更基本。我想他只是想要表1中数据表格式的数据,但没有意识到填充已经完成了。@Mark Brittingham-明白了…Gopal-谢谢你选择我的答案作为答案。-1-你为什么要这样做?第二个cmd只是第一个cmd的重新运行,不起任何作用。而且,即使有一些防御,你也有一组额外的单引号。@Sachin。我使用了您的查询,它将错误显示为表附近的不正确语法。
   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;
        }