2011-12-15 105 views
0

我有一个将数据插入数据库的表单。asp.net SQL Server插入语句tidyup +错误

有一些不总是需要的领域。 当我在代码中留下这些空白时,我收到一条错误消息。

列名或提供的值的数量与表 的定义不匹配。

这是我如何进行数据库设置。 SQL Server 2008

[youthclubid] 
[youthclubname] 
[description] 
[address1] 
[address2] 
[county] 
[postcode] 
[email] 
[phone] 

这是我连接到数据库并执行插入的代码。

connection.Open(); 
cmd = new SqlCommand("insert into youthclublist values ('" + youthclubname.Text + "', '" + description.Text + "','" + address1.Text + "','" + address2.Text + "', '" + county.Text + "', '" + postcode.Text + "', '" + email.Text + "', '" + phone.Text + "')", connection); 
cmd.ExecuteNonQuery(); 
+1

你取得了你的PK列`youthclubid`为`身份=是吗? http://msdn.microsoft.com/en-us/library/ms177173.aspx – 2011-12-15 10:20:59

+1

http://xkcd.com/327/:D – 2011-12-15 10:44:15

回答

0

你需要告诉你要插入像

 insert into youthclublist(youthclubid, youthclubname, ....) values ('" + youthclubname.Text + "', '" + description.Text + "'..... 

哪个字段SQL服务器和你的罚款。

1

这没有办法做你使用SqlParameter这种语句的代码。

所以,你的代码像

SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Northwind_ConnectionString"].ConnectionString); 

     //Create Command object 
     SqlCommand nonqueryCommand = thisConnection.CreateCommand(); 

     try 
     { 

      // Create INSERT statement with named parameters 
      nonqueryCommand.CommandText = "INSERT INTO Employees (FirstName, LastName) VALUES (@FirstName, @LastName)"; 

      // Add Parameters to Command Parameters collection 
      nonqueryCommand.Parameters.Add("@FirstName", SqlDbType.VarChar, 10); 
      nonqueryCommand.Parameters.Add("@LastName", SqlDbType.VarChar, 20); 


      nonqueryCommand.Parameters["@FirstName"].Value = txtFirstName.Text; 
      nonqueryCommand.Parameters["@LastName"].Value = txtLastName.Text; 

      // Open Connection 
      thisConnection.Open(); 

      nonqueryCommand.ExecuteNonQuery(); 
     } 

     catch (SqlException ex) 
     { 
      // Display error 
      lblErrMsg.Text = ex.ToString(); 
      lblErrMsg.Visible = true; 
     } 

     finally 
     { 
      // Close Connection 
      thisConnection.Close(); 

     } 
+0

他问他的错误的解决方案,你不需要总是SQLParameters,其直到开发人员。 – codeSetter 2011-12-15 10:22:20

+1

@Dipak Goswami - 我的系统挂断了这是你不能张贴示例................现在更新 – 2011-12-15 10:26:12

+0

是的,你的代码现在有意义...... – codeSetter 2011-12-15 10:28:35

5

你有两个主要问题:

1)串联起来你的SQL语句很容易受到SQL注入攻击 - 不这样做,使用参数化查询,而不是

2)你不能确定其中列要插入到表中 - 默认情况下,这将是所有列,并且如果不提供所有列的值,则会看到您看到的错误。

我的推荐:总是使用参数化查询并明确定义INSERT语句中的列。这样,您可以定义哪些参数具有值,哪些参数不具有值,并且避免了注入攻击 - 而且您的性能也会更好!

string insertStmt = 
     "INSERT INTO dbo.YouthClubList(Youthclubname, [Description], " + 
     "address1, address2, county, postcode, email, phone) " + 
     "VALUES(@Youthclubname, @Description, " + 
     "@address1, @address2, @county, @postcode, @email, @phone)"; 

using(SqlConnection connection = new SqlConnection(.....)) 
using(SqlCommand cmdInsert = new SqlCommand(insertStmt, connection)) 
{ 
    // set up parameters 
    cmdInsert.Parameters.Add("@YouthClubName", SqlDbType.VarChar, 100); 
    cmdInsert.Parameters.Add("@Description", SqlDbType.VarChar, 100); 
    .... and so on 

    // set values - set those parameters that you want to insert, leave others empty 
    cmdInsert.Parameters["@YouthClubName"].Value = .......; 

    connection.Open(); 
    cmdInsert.ExecuteNonQuery(); 
    connection.Close(); 
} 
1

第一个主要问题是您要连接查询中的输入。这使得您的应用程序极易受到SQL Injection的影响。不要这样做。使用parametrized query

的INSERT语句中的常规语法是这样的:

Insert into <TableName> (Col1, Col2...Coln) values (val1, val2...valn) 

如果需要只插入选定的一组列,您需要提供您插入数据列的列表到列名单。

如果您未指定列列表,则表示您正在将数据插入到每个列表中。

所以你可以检查输入,如果它不存在,你可以省略相应的列。

其他更好的方法是使用存储过程。这将缓解这个问题。

0

尽管新进入编程,我知道插入数据库的最简单方法是创建一个“保存”存储过程,然后通过连接字符串调用该过程。相信我,这是最好的方式。

0

另一种解决方法是使用LINQ to SQL。我发现这更容易。按照这个步骤。

  1. 为您的项目添加一个新的LINQ to SQL类。确保文件扩展名是'.dbml'。将它命名为“YouthClub.dbml”
  2. 使用服务器资源管理器将数据库连接到Visual Studio
  3. 将您的表拖到OR设计器(我不允许发布图像)。

  4. 现在,您可以保存到数据库中使用此代码

    //Create a new DataContext 
        YouthClubDataContext db = new YouthClubDataContext(); 
    
        //Create a new Object to be submitted 
        YouthClubTable newYouthClubRecord = new YouthClubTable(); 
    
        newYouthClubRecord.youthlubname = txtyouthclubname.Text; 
        newYouthClubRecord.description = txtdescription.Text; 
        newYouthClubRecord.address1 = txtaddress1.Text; 
        newYouthClubRecord.address2 = txtaddress2.Text; 
        newYouthClubRecord.county = txtcounty.Text; 
        newYouthClubRecord.email = txtemail.Text; 
        newYouthClubRecord.phone = txtphone.Text; 
        newYouthClubRecord.postcode = txtpostcode.Text; 
    
        //Submit to the Database 
        db.YouthClubTables.InsertOnSubmit(newYouthClubRecord); 
        db.SubmitChanges(); 
    

希望这一次我给一个真正的答案