无法将值插入到表中

本文关键字:插入 | 更新日期: 2023-09-27 18:37:22

我正在将值插入表中,下面是我的代码

 protected void Button4_Click(object sender, EventArgs e)
    {
        string conn = ConfigurationManager.ConnectionStrings["mineConnection"].ConnectionString;
        SqlConnection conn1=new SqlConnection(conn);
        conn1.Open();
       SqlCommand cmd1=new SqlCommand("insert into emp values(@empid,@name)",conn1);
      cmd1.Parameters.Add("@empid", TextBox11.Text);
      cmd1.Parameters.Add("@name", TextBox12.Text);
        SqlDataReader dr;
        SqlCommand cmd=new SqlCommand("select * from emp where empid='"+TextBox11.Text+"'",conn1);
        dr = cmd.ExecuteReader();
        if(dr.HasRows)
        {
         if(dr.Read())
         {
           if(TextBox11.Text==dr[0].ToString())
           {
             Response.Write("id already exists");
           }
         }
        }
        else
        {
          dr.Close();
            cmd.ExecuteNonQuery();
            Response.Write("values inserted");
        }
  }
    }

但问题是我无法插入值,也没有收到任何错误。谁能帮我?

无法将值插入到表中

因为你没有在代码中的任何位置执行 SqlCommand cmd1

定义命令后

  SqlCommand cmd1=new SqlCommand("insert into emp values(@empid,@name)",conn1);
  cmd1.Parameters.Add("@empid", TextBox11.Text);
  cmd1.Parameters.Add("@name", TextBox12.Text);

执行查询

cmd1.ExecuteNonQuery();

我在代码中没有看到您实际执行插入命令的行。

您定义了 cmd1 命令,但从未使用它,因此不会因此插入数据。