我試圖找到最近幾天的這個錯誤,但沒有任何成功。將聲明插入到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)
);
我不知道出了什麼問題。誰能幫忙?
你使用的是什麼版本的SQL服務器? – 2013-02-14 15:11:30
@RyanGates看他的連接字符串 - MS SQL 2012(v11) – t3hn00b 2013-02-14 15:12:59
你在哪裏添加參數到'INSERT'命令? 'cmd.Parameter.Add(「Price」,SqlDbType.Int).Value = 1;' – t3hn00b 2013-02-14 15:21:26