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

我已经把 Using system.data; 并使用 system.data.sql;但它仍然一样。错误:1.找不到类型或命名空间名称“SqlConnection”(是否缺少 using 指令或程序集引用?) 2.找不到类型或命名空间名称“SqlConnection”(是否缺少 using 指令或程序集引用?) 3.名称“ConfigurationManager”在当前上下文中不存在 4.找不到类型或命名空间名称“SqlCommand”(您是否缺少 using 指令或程序集引用?) 5.The找不到类型或命名空间名称“SqlCommand”(您是否缺少 using 指令或程序集引用?)

希望这可以帮助您找到解决我的错误的方法。谢谢

4

5 回答 5

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

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

于 2012-07-17T06:32:56.167 回答
0

你使用什么样的数据库程序?因为就像上面说的 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();
于 2014-05-22T08:49:25.463 回答
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.
于 2012-07-17T06:36:10.643 回答
0

System.Data.Sql 用于 SqlServer

http://msdn.microsoft.com/en-us/library/system.data.sql.aspx

System.Data.Sql 命名空间包含支持 SQL Server 特定功能的类。

使用此处找到的 ADO.NET 驱动程序: http ://www.mysql.com/products/connector/

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

于 2012-07-17T06:46:03.060 回答
0

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

于 2012-07-17T06:32:12.947 回答