简体   繁体   English

C#-从SQL的两个表中选择数据到数据集

[英]C# - Select Data from two tables in SQL to Dataset

I have two tables in my database, (Users and Cities) and I want to select all the data in this tables where the column UserID=1 in Users table. 我的数据库中有两个表(“用户”和“城市”),我想选择此表中的所有数据,其中“用户”表中的UserID = 1列。

But the Dataset does not find my tables (Users and Cities) 但是数据集找不到我的表(用户和城市)

This is my SQL Query: 这是我的SQL查询:

SELECT * FROM Users INNER JOIN Cities ON Cities.CityID=Users.CityID WHERE Users.UserID=1

And this is the Mathod: 这是Mathod:

public static DataSet GetData(string SqlQuery)
{
    OleDbConnection con = new OleDbConnection(conString);
    OleDbCommand cmd = new OleDbCommand(SqlQuery, con);
    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    return ds;
}

Code: 码:

    DataSet ds = GetData(myQuery);

    string fname = ds.Tables["Users"].Rows[0]["UserFisrtName"].ToString();
    string lname = ds.Tables["Users"].Rows[0]["UserLastName"].ToString();
    string city = ds.Tables["Cities"].Rows[0]["CityName"].ToString();

    string output = "Name: " + fname + " " + lname + " City: " + city;

If you want 2 datatables in the data set, change the sql query to this. 如果要在数据集中添加2个数据表,请将sql查询更改为此。

SELECT * FROM Users WHERE Users.UserID=1; select * from City where CityID in (Select cityid from users where userID = 1);

The user table will then be on ds.Tables[0] and the city table on ds.Tables[1]. 然后,用户表将在ds.Tables [0]上,城市表将在ds.Tables [1]上。

Please remember to use the using clause to ensure connections, etc are properly disposed. 请记住使用using子句以确保正确放置连接等。

public static DataSet GetData(string SqlQuery)
{
    using(var con = new OleDbConnection(conString))
    using(var cmd = new OleDbCommand(SqlQuery, con))
    using(var da = new OleDbDataAdapter(cmd))
    {
        var ds = new DataSet();
        da.Fill(ds);
        return ds;
    }
}

Code: 码:

var ds = GetData(myQuery);
var fname = ds.Tables[0].Rows[0]["UserFisrtName"].ToString();
var lname = ds.Tables[0].Rows[0]["UserLastName"].ToString();
var city = ds.Tables[1].Rows[0]["CityName"].ToString();
var output = "Name: " + fname + " " + lname + " City: " + city;

我在sql查询中找到了答案

SELECT * FROM Users, Cities WHERE Users.CityID=Cities.CityID AND Users.UserID=1

In a Dataset your tables will not automatically have the names of the tables from which they are selected. 在数据集中,表将不会自动具有从中选择表的名称。 Use the ordinals instead: 改用普通字母:

DataSet ds = GetData(myQuery);
string fname = ds.Tables[0].Rows[0]["UserFisrtName"].ToString();
string lname = ds.Tables[0].Rows[0]["UserLastName"].ToString();
string city = ds.Tables[0].Rows[0]["CityName"].ToString();

string output = "Name: " + fname + " " + lname + " City: " + city;

You are filling the dataset only with one table, this because the Sql statement only return a one set of data. 您仅用一个表填充数据集,这是因为Sql语句仅返回一组数据。 For access the tables you use indexes, only the columns name will be mapped in the rows of the table so this values can be access by index or by name. 为了访问使用索引的表,只有列名会映射到表的行中,因此可以按索引或按名称访问此值。

SELECT * FROM Users INNER JOIN Cities ON Cities.CityID=Users.CityID WHERE Users.UserID=1

Also avoid use SELECT * this because you force the database engine to search for the columns of all the tables instead to only use the you already provided, and if you will not use all or someone change te order of it in the table, can be a future problem. 还要避免使用SELECT *这是因为您强迫数据库引擎搜索所有表的列,而不是仅使用已经提供的列,并且如果您不使用全部或有人更改表中的顺序,则可以未来的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM