0

我的表结构如下:

Session
--------------
SessionID (PK)
RoomID     
SessionDate
SessionTimeStart
SessionTimeEnd

我有一个以下查询,它将始终返回一行并显示在 DGV 中。我使用 DataAdapter 进行连接:

        DataTable queryResult = new DataTable();
        string ConnStr = "Data Source=DUZY;Initial Catalog=AutoRegSQL;Integrated Security=True";

        SqlConnection MyConn = new SqlConnection(ConnStr);
        MyConn.Open();

        //SQL query that returns todays sessions for the given roomID
        string query = @"SELECT SessionID, RoomID, SessionDate, SessionTimeStart, SessionTimeEnd" +
           " FROM [Session] " +
           " WHERE RoomID = @RoomID " +
           " AND SessionDate = cast(getdate() as date) ";

        SqlCommand command = new SqlCommand(query, MyConn);

        command.Parameters.Add("RoomID", SqlDbType.Char).Value = RoomID;


        SqlDataAdapter adapter = new SqlDataAdapter(command);

        adapter.Fill(queryResult);

我想将查询结果保存到表示表列的多个字符串中,即

SessionIDstring = query result for SessionID column
RoomIDstring = query result for RoomID column
and so on...

是否可以使用一个查询来实现它,还是我必须为每列创建 5 个查询?

4

3 回答 3

2

类似的东西,也许,使用 ADO.NET?

    //SQL query that returns todays sessions for the given roomID
string query = @"SELECT SessionID, RoomID, SessionDate, SessionTimeStart, SessionTimeEnd" +
                " FROM [Session] " +
                " WHERE RoomID = @RoomID " +
                " AND SessionDate = cast(getdate() as date) ";

using (var connection = new SqlConnection(ConnStr))
using (var command = new SqlCommand(query, connection))
{
    command.Parameters.Add("RoomID", SqlDbType.Char).Value = RoomID;

    try
    {
        connection.Open();

        using (SqlDataReader reader = command.ExecuteReader())
        {
            if (reader.Read())
            {
                // Note that reader[x] has the equivalent type to the type
                // of the returned column, converted using
                // http://msdn.microsoft.com/en-us/library/cc716729.aspx
                // .ToString() if the item isn't null is always ok
                string SessionIDstring = reader[0].ToString(); // it should be an int

                // reading it twice is ok
                int RoomID = (int)reader[1]; // it should be an int
                string RoomIDstring = reader[1].ToString(); // it should be an int

                if (reader.Read())
                {
                    throw new Exception("Too many rows");
                }
            }
            else 
            {
                throw new Exception("No rows");
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

此代码改编自MSDN ADO.NET 代码示例。我添加了一些usings 并使其成为单行。我什至不想知道为什么 MSDN 示例没有完整地使用using.

请注意,它SqlDataAdapter是为恢复多行/大数据而构建的,并将它们放在DataSet. 您可以将它们用于单行数据,但SqlDataReader如果您只想填充一些变量,则简单地使用 a 会容易得多。

于 2013-08-15T13:55:38.280 回答
1
declare @col1 int
declare @col2 varchar(42)

select  @col1 = col1
,       @col2 = col2
,       ....
于 2013-08-15T13:50:42.787 回答
1

你可以像这样创建一个类......

public class SessionDto
{
    public string SessionID {get; set;}  
    public string RoomID {get; set;}     
    public string SessionDate {get; set;} 
    public string SessionTimeStart {get; set;} 
    public string SessionTimeEnd {get; set;} 
}

然后有一个方法来获取房间 ID 并构建您的会话对象

public SessionDto GetSessionData(int roomId)
{
    using (var cnn = new SqlConnection(ConnStr))
    {
        SessionDto sessionDto;

        string query = @"SELECT SessionID, RoomID, SessionDate, SessionTimeStart, SessionTimeEnd" +
       " FROM [Session] " +
       " WHERE RoomID = @RoomID " +
       " AND SessionDate = cast(getdate() as date) ";


        cnn.Open();
        using (var cmd = new SqlCommand(query,cnn))
        {
            cmd.Parameters.Add("@RoomID", SqlDbType.Char).Value = roomId;

            using (var rdr = cmd.ExecuteReader())
            {
                if (rdr.HasRows)
                {
                    while (rdr.Read())
                    {
                        sessionDto = new sessionDto{
                            SessionID = rdr.GetString(0),
                            RoomID = rdr.GetString(1),
                            SessionDate = rdr.GetString(2),
                            SessionTimeStart = rdr.GetString(3),
                            SessionTimeEnd = rdr.GetString(4)
                        };
                    }
                }
            }
        }
    }
    return sessionDto;
}

其中很多都是手工输入的,因为我现在无法访问 VS,但你应该让它工作。

另外,我用过rdr.GetString(),还有其他方法GetType()

于 2013-08-15T14:12:08.820 回答