【问题标题】:Insert data into database table with text box使用文本框将数据插入数据库表
【发布时间】:2023-03-17 03:50:01
【问题描述】:

我正在尝试将数据插入表中,而我看到的代码片段似乎对那个人有用,但对我来说! 我不知道我做错了什么,因为我不知道 asp.net 的数据库处理。有人可以告诉我代码有什么问题吗?

public partial class CompanyLogin : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
    OdbcConnection conn = new OdbcConnection();
    conn.ConnectionString = @".\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\VCtemps.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

    string sql = "insert into company values(@CompName, @BusinessType, @Pword)";
    OdbcCommand cmd = new OdbcCommand(sql);
    string CompName = txtCompName.Text;
    string BusinessType = DropDownList1.Text;
    string Pword = txtPassword.Text;
    cmd.Connection = conn;

    cmd.CommandText = "insert into company(CompName, BusinessType, Pword) Values(@CompName,@BusinessType,@Pword);";
    cmd.Parameters.AddWithValue("@CompName",SqlDbType.VarChar);    
    cmd.Parameters.AddWithValue("@BusinessType",SqlDbType.VarChar);    
    cmd.Parameters.AddWithValue("@Pword",SqlDbType.VarChar);    

cmd.ExecuteNonQuery();

    conn.Close();

    txtCompName.Text = "";
    txtPassword.Text = "";
    DropDownList1.Text = "";
}
}

感谢你们,我修复了代码,但是当我运行它或单击注册按钮时,我收到以下错误

ExecuteNonQuery 需要一个开放且可用的连接。连接的当前状态为关闭

【问题讨论】:

  • 你的插入命令应该被重构尝试使用 SQL 参数来代替..

标签: c# asp.net sql-server


【解决方案1】:

您可以调整您的查询 - by deleting values

 cmd.CommandText = "insert into company(CompName, BusinessType, Pword) values('"+ CompName + "','"+ BusinessType + "','" + Pword + "')

注意:我建议你也使用SqlCommand.Parameters.AddWithValue method

并添加此代码:

    cmd.CommandText =  "insert into company(CompName, BusinessType, Pword) Values(@CompName,@BusinessType,@Pword);"

    cmd.Parameters.AddWithValue("@CompName",);    
    cmd.Parameters.AddWithValue("@BusinessType",);    
    cmd.Parameters.AddWithValue("@Pword",);    

    cmd.ExecuteNonQuery();

【讨论】:

  • Candie 的回复比其他人给你的 +1 更清晰
  • 我建议他重构他的查询以使用 Params,有趣的是其他人如何遵循相同的有缺陷的方法来响应我无法忍受硬编码查询,尤其是所有该死的引号和双引号..LOL
【解决方案2】:

尝试更改以下内容:

conn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\VCtemps.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

收件人:

conn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\VCtemps.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

还有:

cmd.CommandText = "insert into company values(CompName, BusinessType, Pword) values('"+ CompName + "','"+ BusinessType + "','" + Pword + "')

收件人:

cmd.CommandText = "insert into company values(CompName, BusinessType, Pword) values('"+ CompName + "','"+ BusinessType + "','" + Pword + "')";

补充:

您应该使用参数化查询。切勿将用户输入直接传递到 SQL 语句中,因为您将容易受到 SQL 注入攻击。

string commandText = "insert into company values(CompName, BusinessType, Pword) values(@CompName, @BusinessType, @Pword)";
SqlCommand command = new SqlCommand(commandText, connection);

command.Parameters.Add("@CompName", SqlDbType.VarChar);
command.Parameters.Add("@BusinessType", SqlDbType.VarChar);
command.Parameters.Add("@PWord", SqlDbType.VarChar);

【讨论】:

    【解决方案3】:

    cmd.CommandText = "插入公司值(CompName, BusinessType, Pword) values('"+ CompName + "','"+ BusinessType + "','" + Pword + "'");

    试试这个....

    【讨论】:

      【解决方案4】:

      检查以cmd.CommandText 开头的行是否有引号中的错误。

      您可以尝试改用 String.Format 方法,如下所示:

       cmd.CommandText = String.Format("insert into company values(CompName, BusinessType, Pword) values('{0}','{1}','{2}')",CompName,BusinessType,Pword);
      

      我发现这可以帮助我更轻松地跟踪连接变量。

      【讨论】:

        【解决方案5】:
        1. 以后代码在sql命令中使用参数!!见example
        2. 在整个字符串前使用@ 转义连接字符串,或在需要转义的符号前使用'\'。 Example
        3. 最后cmd.CommandText = "insert into company values(CompName, BusinessType, Pword) values('"+ CompName + "','"+ BusinessType + "','" + Pword + "') 不见了";
        4. string sql = "insert into company values(@CompName, @BusinessType, @Pword)"; OdbcCommand cmd = new OdbcCommand(sql);

          cmd.CommandText = "insert into company values(CompName, BusinessType, Pword) values('"+ CompName + "','"+ BusinessType + "','" + Pword + "')
          两者都是setting CommandText,所以你可以删除sql 并将
          OdbcCommand cmd = new OdbcCommand(sql); 更改为 OdbcCommand cmd = new OdbcCommand();

        【讨论】:

          【解决方案6】:
          1. 连接未打开
          2. 带参数查询的不必要的 sql 字符串
          3. 查询中的语法错误 (CommandText)

          .

          protected void Button1_Click(object sender, EventArgs e)
          {
              OdbcConnection conn = new OdbcConnection();
              conn.ConnectionString = "Data Source=.\SQLEXPRESS;
                                 AttachDbFilename=|DataDirectory|\VCtemps.mdf;Integrated 
                                 Security=True;Connect Timeout=30;User Instance=True";
          
              OdbcCommand cmd = new OdbcCommand();
              string CompName = txtCompName.Text;
              string BusinessType = DropDownList1.Text;
              string Pword = txtPassword.Text;
          
              conn.Open();
              cmd.Connection = conn;
          
              cmd.CommandText = "insert into company (CompName, BusinessType, Pword) 
                          values('"+ CompName + "','"+ BusinessType + "','" + Pword + "')";
          
              cmd.ExecuteNonQuery();
          
              conn.Close();
          
              txtCompName.Text = "";
              txtPassword.Text = "";
              DropDownList1.Text = "";
          }
          

          【讨论】:

            【解决方案7】:

            检查以下示例。还将您的连接和命令包装在using clause

              string yourConnectionString="";
                int result=0;
                using(OdbcConnection conn = new OdbcConnection(yourConnectionString))
                {
            
                     string sql = "insert into company values(@CompName, @BusinessType, @Pword)";
                     using (OdbcCommand cmd=new OdbcCommand(sql,conn))
                     {   
                        cmd.Parameters.AddWithValue("@CompName",txtCompName.Text);
                        cmd.Parameters.AddWithValue("@BusinessType",DropDownList1.SelectedValue);  
                        cmd.Parameters.AddWithValue("@Pword ",txtPassword.Text);  
                        conn.Open();
                        result=cmd.ExecuteNonQuery();
                     }
                     conn.Close();
                     if(result >0)
                     {
                       txtCompName.Text = "";
                       txtPassword.Text = "";
                       DropDownList1.SeletedIndex = -1;
                     }    
                }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2011-05-05
              • 1970-01-01
              • 2012-01-20
              • 2013-03-27
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多