2013-02-14 77 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

相关问题