2017-07-27 58 views
0

僅供參考,此頁面(add.ashx.cs)是數據庫的添加頁面。C#如何執行一個字符串,然後存儲該結果?

我試圖做的是:

  1. 弄清楚如何執行字符串queryID和queryID

的那麼

  • 店,結果我有點新意這,但這是我迄今爲止正在處理的內容。我在正確的道路上,我應該改變什麼?我不相信下面的代碼包括存儲結果,但只是執行queryID。

    // new query to get last ID value 
    // store the command.executeNonQuery results into a variable 
    
    string queryID = "SELECT TOP (1) IDENT_CURRENT('dbo.license_info') FROM dbo.license_info"; 
    // first: look up how to execute queryID 
    // then: store results of query^
    
    // execute queryID? (section below) 
    SqlConnection sqlConnection1 = new SqlConnection(queryID); 
    SqlCommand cmd = new SqlCommand(); 
    SqlDataReader reader; 
    cmd.CommandText = "Select * FROM queryID"; 
    cmd.CommandType = CommandType.Text; 
    cmd.Connection = sqlConnection1; 
    
    sqlConnection1.Open(); 
    
    reader = cmd.ExecuteReader(); 
    // data is accessible through the datareader object here 
    
    sqlConnection1.Close(); 
    
  • +0

    您正試圖連接到其連接字符串爲某個SQL的數據庫。如果你想執行'queryID',那麼你應該把它放在'CommandText'中(並且將實際的連接字符串傳遞給'SqlConnection'而不是'QueryID') – litelite

    +0

    請閱讀關於SqlConnection的文檔以及構造函數 –

    回答

    3

    在您的代碼示例中有一些內容不匹配。第一個queryID是您的實際查詢。其次,在SqlConnection中,您需要提供一個連接字符串,它連接到您的數據庫(SQL Server,ACCESS,...)。一個有效的例子可能是這樣的:

    // this is just a sample. You need to adjust it to your needs 
        string connectionStr = "Data Source=ServerName;Initial Catalog=DataBaseName;Integrated Security=SSPI;"; 
        SqlConnection sqlConnection1 = new SqlConnection(connectionStr); 
        SqlCommand cmd = new SqlCommand(sqlConnection1); 
        SqlDataReader reader; 
    
        cmd.CommandText = "SELECT TOP (1) IDENT_CURRENT('dbo.license_info') FROM dbo.license_info"; 
        cmd.CommandType = CommandType.Text; 
        sqlConnection1.Open(); 
    
        reader = cmd.ExecuteReader(); 
    
        List<string> results = new List<string>(); 
    
        if(reader.HasRows) 
        { 
         while(reader.Read()) 
         { 
          results.Add(reader[0].ToString()); 
         } 
        } 
    
        sqlConnection1.Close(); 
    

    另一件事是,你執行一個閱讀器,但只選擇一個單一的值。您可以完美地使用ExecuteScalar

    // this is just a sample. You need to adjust it to your needs 
    string connectionStr = "Data Source=ServerName;Initial Catalog=DataBaseName;Integrated Security=SSPI;"; 
    SqlConnection sqlConnection1 = new SqlConnection(connectionStr); 
    SqlCommand cmd = new SqlCommand(sqlConnection1); 
    
    cmd.CommandText = "SELECT TOP (1) IDENT_CURRENT('dbo.license_info') FROM dbo.license_info"; 
    cmd.CommandType = CommandType.Text; 
    sqlConnection1.Open(); 
    
    string result = cmd.ExecuteScalar().ToString(); 
    
    sqlConnection1.Close(); 
    

    最後一件事。您應該在使用塊中使用實現IDisposable的對象。這種方式將在不再需要時從內存中移除:

    // this is just a sample. You need to adjust it to your needs 
    string connectionStr = "Data Source=ServerName;Initial Catalog=DataBaseName;Integrated Security=SSPI;"; 
    using(SqlConnection sqlConnection1 = new SqlConnection(connectionStr)) 
    { 
        SqlCommand cmd = new SqlCommand(sqlConnection1); 
    
        cmd.CommandText = "SELECT TOP (1) IDENT_CURRENT('dbo.license_info') FROM dbo.license_info"; 
        cmd.CommandType = CommandType.Text; 
        sqlConnection1.Open(); 
    
        string result = cmd.ExecuteScalar().ToString(); 
    } 
    
    +0

    期望的內容while這個答案是正確的,這個代碼需要一些改進。 1.查詢本身可以簡化爲'SELECT IDENT_CURRENT('dbo.license_info')'2.在using語句中使用所有的IDisposable實例 - SqlConnection,SqlCommand和SqlDataReader都實現了IDisposable。 –

    +0

    @ZoharPeled你是對的,我仍然在編輯我的答案與改進(但我錯過了SQL改進) –

    +0

    我upvoted它無論如何:-) –

    相關問題