2012-07-17 103 views
-2

我的代码有什么问题?当我设置我的SQL和ASP之间的连接时,它给了我这个错误:无法找到sqlcommand。您是否错过......”设置连接时出错

这里是我的代码

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 
using System.Data.Sql; 

    protected void Button2_Click(object sender, EventArgs e) 
{ 
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString); 
    SqlCommand cmd = new SqlCommand("Insert into CarTab(Brand,Model,Plate,Color,Service) Values (@brand,@model,@plate,@color,@year,@service)",conn); 

    cmd.CommandType = CommandType.Text; 
    cmd.Parameters.AddWithValue("@brand", Label1.Text); 
    cmd.Parameters.AddWithValue("@model", Label2.Text); 
    cmd.Parameters.AddWithValue("@plate", Label3.Text); 
    cmd.Parameters.AddWithValue("@color", Label4.Text); 
    cmd.Parameters.AddWithValue("@year", Label5.Text); 
    cmd.Parameters.AddWithValue("@service", Label6.Text); 

    conn.Open(); 
    cmd.ExecuteNonQuery(); 
} 

我已经把使用system.data;和使用system.data.sql;但它仍然是相同的 错误: 1.无法找到类型或名称空间名称'SqlConnection'(缺少使用指令或程序集引用吗?) 2.无法找到类型或名称空间名称'SqlConnection'(是否缺少使用指令或程序集引用?) 3.名称'ConfigurationManager'在当前上下文中不存在 4.类型或名称空间名称'SqlCommand'co (找不到使用指令或程序集引用?) 5.无法找到类型或名称空间名称'SqlCommand'(您是否缺少使用指令或程序集引用?)

Hope这有助于您找到解决我的错误的方法。谢谢

+0

你用'sqlcommand'如在味精somwhere所示的一样呢? – V4Vendetta 2012-07-17 06:36:50

+0

不要忘记你的'使用'块! – 2012-07-17 06:42:43

回答

0

您是否添加了对System.Data程序集的引用?

+0

如何添加?你能告诉我吗? – aemy 2012-07-17 06:39:05

+0

它是:http://msdn.microsoft.com/en-us/library/7314433t(v=vs.90).aspx – Novakov 2012-07-17 06:41:13

1

两件事。你还没有关闭你的SQL命令:

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString); 
SqlCommand cmd = new SqlCommand("Insert into CarTab") 

第二,你没有任何合格的数据插入到你的CarTab表?您需要指定字段和值:

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString); 
SqlCommand cmd = new SqlCommand("Insert into CarTab(field1, field2) VALUES('val1', 12)") 

还有一些其他的方法来插入数据 - 如INSERT SELECT:

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString); 
SqlCommand cmd = new SqlCommand("Insert into CarTab(field1, field2) SELECT field1, field2 FROM Table2") 

http://www.sqlteam.com/article/using-select-to-insert-records

继的评论,在这里是如何以您指定的方式充分使用ADO的示例:

using System.Data; 
using System.Data.SqlClient; 

      using (var con = new SqlConnection("your connection string")) { 
       con.Open(); 
       using (var com = con.CreateCommand()) { 
        var var1 = "test"; 
        var var2 = "test2"; 
        com.CommandText = string.Format("INSERT INTO Table1(col1, col2) VALUES({0}, {1})", var1, var2); 
        com.CommandType = CommandType.Text; 
        com.ExecuteNonQuery(); 
       } 
       con.Close(); 
      } 

请注意,我没有测试过,但它应该给你一个很好的起跑线。

+0

是的,我知道。但现在的重点是sqlconnection,命令,某处出了问题。 – aemy 2012-07-17 06:40:41

+0

你需要更清楚你的问题并发布完整的错误和答案。 您还需要打开连接(conn.Open())并将其与您的命令(cmd.Connection = conn)关联。我将用完整的代码修改我的答案。 – Spikeh 2012-07-17 06:42:16

0

1)您还没有关闭SqlCommand对象和错误的Sql Insert语句。 这将是

SqlCommand cmd = new SqlCommand("Insert into CarTab(col1,col2,...) VALUES(val1,val2,..)"); 

2)你还没有打开连接,而不是分配像

conn.Open(); 
cmd.Connection = conn; 

3到命令对象的连接),并执行查询后,你必须关闭连接

cmd.ExecuteNonQuery(); 
conn.Close(); // close the connection. 
+0

我认为他得到的错误是在编译时,所以即使conn.Open()部分是正确的,但这不是问题。 – Spikeh 2012-07-17 06:38:14

+0

@Spikeh是的你是对的 – 2012-07-17 06:40:01

0

你使用的是什么样的db程序?因为像grayfox这里说的上面您需要的SQL Server,并尝试做这种方式,工作FO RME:

using (SqlConnection connection = new SqlConnection()) 
     { 
      string connectionStringName = this.DataWorkspace.dbsMSccData.Details.Name; 

      connection.ConnectionString = 
      ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString; 

      string procedure = entity.Procedure; 
      using (SqlCommand command = new SqlCommand(procedure, connection)) 
      { 
       command.CommandType = CommandType.StoredProcedure; 

       //foreach (var item in entity.StoredProcedureParameters) 
       //{ 
       // command.Parameters.Add(
       //  new SqlParameter(item.ParameterName, item.ParameterValue)); 
       //} 

       connection.Open(); 
       command.ExecuteNonQuery(); 
      } 
     } 
     this.Details.DiscardChanges();