C# 设置连接时出错

C# 设置连接时出错,c#,asp.net,sql,C#,Asp.net,Sql,我的代码有什么问题?当我在我的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

我的代码有什么问题?当我在我的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();
}
我已经把Using system.data;和Using system.data.sql;放在一起了,但还是一样的。 错误: 1.找不到类型或命名空间名称“SqlConnection”(是否缺少using指令或程序集引用?) 2.找不到类型或命名空间名称“SqlConnection”(是否缺少using指令或程序集引用?) 3.当前上下文中不存在名称“ConfigurationManager” 4.找不到类型或命名空间名称“SqlCommand”(是否缺少using指令或程序集引用?) 5.找不到类型或命名空间名称“SqlCommand”(是否缺少using指令或程序集引用?)


希望这能帮助您找到解决我的错误的方法。谢谢。您是否添加了对System.Data assembly的引用?

有两件事。您尚未关闭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)")
还有许多其他方法可以插入数据,如插入选择:

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

除了注释之外,下面是一个示例,说明如何以指定的方式充分使用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();
            }
请注意,我还没有测试它,但它应该给您提供一个良好的起点。

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.

System.Data.Sql用于SqlServer

System.Data.Sql命名空间包含支持Sql的类 特定于服务器的功能

使用以下ADO.NET驱动程序:


或者使用ODBC(不是首选选项)。

您使用的是哪种数据库程序?正如grayfox在上文中所说,您需要sql server,并尝试以这种方式执行此操作,因此成功地:

 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();

您是否使用了与msg somwhere相同的
sqlcommand
?不要忘记使用
块的
!我认为他得到的错误是在编译时,因此即使conn.Open()部分是正确的,这不是这里的问题。如何添加它?你能告诉我吗?是的,我知道。但现在的问题是sqlconnection,command,某些地方出了问题。你需要更清楚地回答你的问题,并发布完整的错误和答案。你还需要打开你的连接(conn.open()),并将其与你的命令(cmd.connection=conn)关联.我会用完整的代码修改我的答案。