【问题标题】:How do I execute an INSERT using SqlCommand?如何使用 SqlCommand 执行 INSERT?
【发布时间】:2010-06-20 00:37:38
【问题描述】:

我还有另一个class that creates the connection(请参阅页面下半部分的精致课程),我相信它可以连接,或者至少我在运行时没有遇到任何异常。

我现在想将数据插入到我的数据库中,但没有任何更新。 我正在尝试关注this 示例,但它是为了阅读。

我确实在显示 SQL 窗格上运行了“querystring”,它确实执行正确。它将数据作为新行正确插入。

不知道在我的最后一行之后该做什么?谢谢。

string queryString =
"INSERT INTO UserData UserProfileID, ConfidenceLevel, LoveLevel, HappinessLevel) VALUES ('a051fc1b-4f51-485b-a07d-0f378528974e', 2, 2, 2);";

    protected void Button1_Click(object sender, EventArgs e)
    {
        MySqlConnection database = new MySqlConnection();
        database.CreateConn();

        Command = new SqlCommand(queryString, database.Connection);  //

    }

【问题讨论】:

    标签: asp.net html windows database


    【解决方案1】:
     Command.ExecuteNonQuery();
    

    顺便说一句,您的 SQL 查询字符串中缺少括号。

    当然,您会在实际查询中使用参数(并且您不会使用字符串连接而不是传递参数,因为它会为破坏性 SQL 注入攻击打开大门)。

    【讨论】:

      猜你喜欢
      • 2013-07-08
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 2016-09-11
      • 2013-12-18
      • 1970-01-01
      相关资源
      最近更新 更多