在过去的几天里,我试图找到这个错误,但没有任何成功。
我正在尝试在数据库中插入一个新行。一切顺利:没有错误,也没有程序崩溃。
我的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();
正如我已经提到的,看起来程序运行了这个查询,一切都很正常,但是当我查看 tableRacun
时,没有新行。此外,当我尝试刷新此表时,data 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)
);
我不知道出了什么问题。任何人都可以帮忙吗?