2016-02-17 60 views
0

我创建了一个使用VS本地数据库的小型c#应用程序这些是我的插入命令,它没有显示任何错误,但字段没有插入到数据库中。插入语句不起作用

public partial class Form1 : Form 
{    
    SqlCeConnection cn=new SqlCeConnection("Data Source=|DataDirectory|\\Database1.sdf"); 

    public Form1() 
    { 
     InitializeComponent(); 
     cn.Open(); 
     SqlCeCommand cmd; 
     string sql = "insert into sales values (@item, @price)"; 
     try 
     { 
      cmd = new SqlCeCommand(sql, cn); 
      cmd.Parameters.AddWithValue("@item", "7777"); 
      cmd.Parameters.AddWithValue("@price"," 2"); 
      cmd.CommandType = CommandType.Text; 
      cmd.ExecuteNonQuery(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
     finally 
     { 
      cn.Close(); 
     } 
    } 
} 

当将这个应用程序部署到用户时,我应该在客户端机器上安装任何东西时还有一个问题?

+3

什么呢cmd.ExecuteNonQuery();返回? –

+0

如何检查它返回的是什么,但我真的是新的 – zozi

+0

@zozi'var whatever = cmd.ExecuteNonQuery();' – Kritner

回答

0

尝试这样的事情

//include column names 
    string sql = "insert into sales(Item,Price) values (@item, @price)"; 

    try 
    { 
    cmd = new SqlCeCommand(sql, cn); 
    cmd.Parameters.AddWithValue("@item", 7777); //int instead of string 
    cmd.Parameters.AddWithValue("@price", 2); //int instead of string 
    cmd.CommandType = CommandType.Text; 
    cmd.ExecuteNonQuery(); 
    }