2013-02-14 75 views
2

我試圖找到最近幾天的這個錯誤,但沒有任何成功。將聲明插入到SQL Server數據庫中

我試圖在數據庫中插入一個新行。一切順利:沒有錯誤,沒有程序崩潰。

INSERT聲明是這樣的:

INSERT INTO Polozaj(Znesek, Uporabnik, Cas, Kupec, Popust, Poln_znesek) 
VALUES(1,1,1,1,1,1) 

該聲明是確定的,因爲當我在我的數據庫運行查詢它增加了新行。

我的C#代碼如下所示:

string connection = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=" + Application.StartupPath + "\\Trgovina.mdf;Integrated Security=True"; 
SqlConnection cn = new SqlConnection(connection); 

string payment = ((Button)sender).Text, maxID = string.Empty; 
double discount = Convert.ToDouble(discauntText.Text), totalPrice = Convert.ToDouble(fullPriceText.Text), fullPrice = Convert.ToDouble(discountPriceText.Text); 
switch (payment) 
{ 
    case "Dobavnica": discount = 10; break; 
    case "Kartica": discount = 0; break; 
    case "Gotovina": discount = 5; break; 
} 

cn.Open(); 

SqlCommand maxIdCmd = new SqlCommand("SELECT MAX(Id_racuna) FROM Racun", cn); 
maxID = Convert.ToString(maxIdCmd.ExecuteScalar()); 
maxID = maxID != "" ? Convert.ToString(Convert.ToInt32(maxID) + 1) : "1"; 

string stmt = "INSERT INTO Racun(Znesek, Uporabnik, Cas, Kupec, Popust, Poln_znesek) " + 
       "VALUES(@Price, @User, @Time, @Customer, @Discount, @FullPrice)"; 

SqlCommand cmd = new SqlCommand(stmt, cn); 
cmd.ExecuteNonQuery(); 

cn.Close(); 

正如我已經提到的,它看起來像程序運行此查詢,一切都只是正常的,但是當我看着表Racun,沒有新的行。此外,當我嘗試刷新此表數據的Visual Studio(2012)給我,看起來像一個錯誤:This database cannot be imported. It is either unsupported SQL Server version or an unsupported database compatibility.

而我的表Racun創建查詢看起來是這樣的:

CREATE TABLE [dbo].[Racun] (
    [Id_racuna] INT    IDENTITY (1, 1) NOT NULL, 
    [Znesek]  NUMERIC (10, 3) NULL, 
    [Uporabnik] NCHAR (20)  NULL, 
    [Cas]   NCHAR (15)  NULL, 
    [Kupec]  NCHAR (10)  NULL, 
    [Popust]  NUMERIC (10, 3) NULL, 
    [Poln_znesek] NUMERIC (10, 3) NULL, 
    PRIMARY KEY CLUSTERED ([Id_racuna] ASC) 
); 

我不知道出了什麼問題。誰能幫忙?

+0

你使用的是什麼版本的SQL服務器? – 2013-02-14 15:11:30

+0

@RyanGates看他的連接字符串 - MS SQL 2012(v11) – t3hn00b 2013-02-14 15:12:59

+0

你在哪裏添加參數到'INSERT'命令? 'cmd.Parameter.Add(「Price」,SqlDbType.Int).Value = 1;' – t3hn00b 2013-02-14 15:21:26

回答

4

沒有爲這樣的刀片三種可能的情況:

  1. 插入成功。
  2. 你會得到一個異常。
  3. 你有一個觸發器,用一些其他操作替換插入。

我想你沒有觸發器,並且你沒有在表中獲得記錄,必須有一個例外。

您是否有任何代碼可以捕獲任何其他級別的異常?這可以解釋爲什麼你看不到它,並且它也會使數據庫連接保持未關閉狀態,這可以解釋爲什麼之後連接到數據庫時出現問題。

使用數據庫連接的using塊即使在代碼中存在錯誤時也會正確關閉它。

您正在使用參數化查詢,但我看不到您將參數添加到代碼中任何位置的命令對象。這將是這樣的:

cmd.Parameters.Add("Price", SqlDbType.Decimal).Value = price; 
cmd.Parameters.Add("User", SqlDbType.NChar, 20).Value = user; 
cmd.Parameters.Add("Time", SqlDbType.NChar, 15).Value = time; 
cmd.Parameters.Add("Customer", SqlDbType.NChar, 10).Value = customer; 
cmd.Parameters.Add("Discount", SqlDbType.Decimal).Value = discount; 
cmd.Parameters.Add("FullPrice", SqlDbType.Decimal).Value = fullPrice; 
+0

這沒有什麼區別.. 你能解釋一些這些例外嗎? – Clem 2013-02-14 15:33:01

2

我會嘗試在try塊包裝你的代碼,看看你能不能趕上SqlExecption

try { 
    SqlCommand cmd = new SqlCommand(stmt, cn); 
    cmd.ExecuteNonQuery(); 
} catch (SqlException ex) { 
    Console.WriteLine(ex.Message); 
} 

編輯:它看起來像你缺少你參數你INSERT語句,你應該看看使用SqlTransaction

SqlCommand maxIdCmd = new SqlCommand("SELECT MAX(Id_racuna) FROM Racun", cn); 
maxID = Convert.ToString(maxIdCmd.ExecuteScalar()); 
maxID = maxID != "" ? Convert.ToString(Convert.ToInt32(maxID) + 1) : "1"; 

string stmt = "INSERT INTO Racun(Znesek, Uporabnik, Cas, Kupec, Popust, Poln_znesek) " + 
       "VALUES(@Price, @User, @Time, @Customer, @Discount, @FullPrice)"; 

SqlCommand cmd = new SqlCommand(stmt, cn); 

// Adding parameters to the insert statement: 
cmd.Parameters.AddWithValue("@Price", price); 
cmd.Parameters.AddWithValue("@User", user); 
cmd.Parameters.AddWithValue("@Time", time); 
cmd.Parameters.AddWithValue("@Customer", customer); 
cmd.Parameters.AddWithValue("@Discount", discount); 
cmd.Parameters.AddWithValue("@FullPrice", fullprice); 

// Start a transaction so we can roll back if there's an error: 
SqlTransaction transaction = cn.BeginTransaction(); 
cmd.Transaction = transaction; 

try { 
    cmd.ExecuteNonQuery(); 
    transaction.Commit(); 
} catch (SqlException ex) { 
    transaction.Rollback(); 
    Console.WriteLine(ex.Message); 
} finally { 
    cn.Close(); 
} 
+0

它不返回錯誤 – Clem 2013-02-14 15:22:08

+0

現在:當分配給該命令的連接處於未決的本地事務中時,ExecuteNonQuery需要該命令進行事務。該命令的Transaction屬性尚未初始化。 – Clem 2013-02-14 15:36:32

+0

@Klemzy對不起,這是我的一個錯字。在創建事務對象後,您需要添加'cmd.Transaction = transaction'行。這裏有更多關於'SqlTransaction'對象的地方:http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction.aspx – 2013-02-14 15:42:17

相關問題