简体   繁体   中英

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.

But the Dataset does not find my tables (Users and Cities)

This is my SQL Query:

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

And this is the 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.

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].

Please remember to use the using clause to ensure connections, etc are properly disposed.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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