2013-08-22 35 views
0

我有問題插入數據到我的數據庫中。 我可以通過使用select查詢從數據庫讀取,所以我知道我的連接字符串是正確的,但出於某種原因插入不起作用。 這裏是我的代碼:SQL c#插入命令不起作用

private string ConnectionString() 
{ 
    return @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\App_Data\dbBusiness.mdf;Integrated Security=True;User Instance=True"; 
} 
private void Insert() 
{ 
    try{ 
     string sqlStrInsert = "INSERT INTO myTable ([param1],[param2])VALUES(@param1,@param2)"; 
     SqlConnection connection = new SqlConnection(ConnectionString()); 
     SqlCommand command = new SqlCommand(sqlStrInsert, connection); 
     command.Parameters.Add("@param1", SqlDbType.SmallInt); 
     command.Parameters.Add("@param2", SqlDbType.NVarChar,50); 
     command.Parameters["@param1"].Value = numOf_company; 
     command.Parameters["@param2"].Value = txt_name.Text; 
     connection.Open(); 
     command.ExecuteNonQuery(); 
     connection.Close(); 
     } 
    catch(Exception ex) 
     { 
     throw new Exception(ex.ToString(), ex); 
     } 
} 

它不顯示任何exeption,當我通過Visual Studio的探險家檢查我的表 沒有被添加到表中。 即時通訊解決這個問題,所以 我會很感謝任何人誰幫助

+2

請告訴我們它是如何不起作用。簡單地說,「它不起作用」並不是**在任何可以接近的地方**足夠的信息。它錯誤嗎?如果是這樣,用什麼?在哪一行? – Arran

+0

你收到了什麼錯誤信息? – Tommassiov

+0

使用try/catch和debug查看你得到了什麼異常 – Zaki

回答

0

相信您忘記您的初始目錄中的連接。因爲沒有它,你的代碼將嘗試在master(默認數據庫)中運行。 下面是一個應該幫助你的代碼片段,你需要明顯地修改代碼以獲得好處。

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Data.SqlClient; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace Working 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     string DsQuery = "INSERT INTO table (param1,param2)VALUES(@param1,@param2)"; 
     string TgtServer = @".\SQLEXPRESS"; 
     DataSet dsServers = new DataSet(); 
     dsServers = ExecQuery(DsQuery, TgtServer, "InitialCatalog"); 
    } 

    private static DataSet ExecQuery(string strQuery, string strServer, string strIC) 
    { 

     string connectionString = @"Data Source=" + strServer + ";Initial Catalog=" + strIC + ";Integrated Security=SSPI"; 
     string commandString = strQuery; 

     DataSet ds = new DataSet(); 
     try 
     { 
      SqlDataAdapter da = new SqlDataAdapter(commandString, connectionString); 
      da.Fill(ds, "Table"); 
     } 
     catch (SqlException e) 
     { 
      Console.WriteLine("An SQL Exception Occured: " + e.Message); 

     } 
     catch (Exception e) 
     { 
      Console.WriteLine("An General Exception Occured: " + e.Message); 

     } 

     return ds; 

    } 
} 

}

對不起,我不能給你任何東西,但是那是後話,我注意到失蹤,並根據給定的,我不能提供任何錯誤。

這裏是進一步閱讀: http://technet.microsoft.com/en-us/library/aa905872(v=sql.80).aspx

+0

在初始目錄中需要寫什麼?它是:| DataDirectory | \ App_Data \ dbBusiness.mdf還是別的嗎? – user2139184

+0

要連接到的數據庫的名稱。 –

+0

我使用了上面寫的但它仍然不起作用 – user2139184

0

誰是TABLE!?!?你的桌子的名字?如果是,請因爲如果保留關鍵字

INSERT INTO table (param1,param2)VALUES(@param1,@param2) 

成爲(例如)改變這個名字

INSERT INTO myTable (param1,param2)VALUES(@param1,@param2) 
+0

不需要改變,只是在它周圍包裹一個'[]'。 –

+0

我當然希望他只是改了問題的名字 – Jonesopolis

+0

@王金:是的,但最好不要使用這個關鍵字 –