【问题标题】:Insert to a SQL Server CE database插入到 SQL Server CE 数据库
【发布时间】:2013-03-27 13:31:53
【问题描述】:

如何插入.sdf 数据库中的表?

我尝试了以下方法:

string connection = @"Data Source=|DataDirectory|\InvoiceDatabase.sdf";
SqlCeConnection cn = new SqlCeConnection(connection);

try
{
   cn.Open();
}
catch (SqlCeException ex)
{
    MessageBox.Show("Connection failed");
    MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
    Application.ExitThread();
}

string clientName = txt_ClientName.Text;
string address = txt_ClientAddress.Text;
string postcode = txt_postcode.Text;
string telNo = txt_TelNo.Text;

string sqlquery = ("INSERT INTO Client (Name,Address,Postcode,Telephone_Number)Values(" + clientName + "','" + address + "','" + postcode + "','" + telNo + ")");
SqlCeCommand cmd = new SqlCeCommand(sqlquery, cn);

try {
  int affectedRows = cmd.ExecuteNonQuery();

  if (affectedRows > 0)
  {
     txt_ClientAddress.Text = "";
     txt_ClientName.Text = "";
     txt_postcode.Text = "";
     txt_TelNo.Text = "";
     MessageBox.Show("Client: " + clientName + " added to database. WOoo");
  }
}
catch(Exception){
    MessageBox.Show("Insert Failed.");
} 

但我做什么似乎并不重要,它只是显示“插入失败”。

提前致谢。

【问题讨论】:

  • 请捕捉具体异常并在此处发布消息

标签: c# insert sql-server-ce


【解决方案1】:

您忘记了第一个值的左引号。

Values(" + clientName + "','"

改为:

Values('" + clientName + "','"

但这通常是构建查询的不好方法。请改用参数化查询。
见:http://msdn.microsoft.com/en-us/library/system.data.sqlserverce.sqlcecommand.parameters(v=vs.80).aspx

catch(Exception ex)
{
   MessageBox.Show(ex);
} 

将为您提供有关错误的更多信息。

【讨论】:

    【解决方案2】:

    这是同样的老故事。当您构建连接字符串的 sql 命令时,这些类型的错误比比皆是。简单的语法问题并不是最糟糕的。 Sql Injection 是最危险的。

    请以这种方式构建您的查询

    string sqlquery = ("INSERT INTO Client (Name,Address,Postcode,Telephone_Number)" + 
                       "Values(@client,@address, @postcode, @tel)";
    SqlCeCommand cmd = new SqlCeCommand(sqlquery, cn);
    cmd.Parameters.AddWithValue("@client", clientName);
    cmd.Parameters.AddWithValue("@address", address);
    cmd.Parameters.AddWithValue("@postcode", postcode);
    cmd.Parameters.AddWithValue("@tel", telNo);
    cmd.ExecuteNonQuery();
    

    正如其他人已经说过的那样,您的语法错误是由于省略了初始单引号引起的。但是你可能有其他错误。例如,名为O'Hara 的客户端呢?现在您在客户端名称中有一个单引号,这会严重破坏您的字符串连接。 取而代之的是一个参数将被准确解析,并且找到的每个有问题的字符都将得到适当的处理(在这种情况下加倍单引号)

    【讨论】:

    • +1000000 如果可以的话 - 完全同意;如果今天还有人还在教学生将他们的 SQL 语句连接在一起 - 他应该被一根羽毛挠死.....
    • @marc_s:我觉得勺子更合适。
    【解决方案3】:

    您的 SQL 语句不正确。

    string sqlquery = ("INSERT INTO Client (Name,Address,Postcode,Telephone_Number)Values('" + clientName + "','" + address + "','" + postcode + "','" + telNo + "')");
    

    拿着这个。您忘记了值开头和结尾的 '

    【讨论】:

      【解决方案4】:

      要向 Sql 中插入数据,应考虑数据类型。如果你插入一个字符串值(varchar)你必须用单引号括起来,比如'"+full_Name+"',但是整数类型不需要这个。例子

      string myQuery = "INSERT INTO Persons (phone, fullname) VALUES ("+telNo+",'"+full_Name+"')";
      

      其中全名是字符串变量,电话号码只是数字。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多