【问题标题】:Not able to insert data into SQL Server through Windows Forms C#无法通过 Windows 窗体 C# 将数据插入 SQL Server
【发布时间】:2017-07-15 11:38:32
【问题描述】:

编译器没有显示任何错误。我想知道它有什么问题。

是的,它也没有显示消息“插入!!”

这是我在 Winforms 中插入的第一个数据。是的,我是新人。

代码:

public partial class Phone : Form
{
    SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=phonemo;Persist Security Info=True;User ID=sa;Password=***********");

    public Phone()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        DialogResult dialogResult = MessageBox.Show("This will Clear Text", "New", MessageBoxButtons.YesNo);

        if (dialogResult == DialogResult.Yes)
        {
            //do something
        }
        else if (dialogResult == DialogResult.No)
        {
            //do something else
        }

        textBox1.Text = "";
        textBox2.Clear();
        textBox3.Text = "";
        textBox4.Clear();
        comboBox1.SelectedIndex = -1;
        textBox1.Focus();

        con.Open();
        String query = "INSERT INTO phonemoo (Fname,Sname,Mobile,Email,Catagory) VALUES ('"+ textBox1.Text +"','"+ textBox2.Text +"','"+ textBox3.Text +"','"+ textBox4.Text +"','"+ comboBox1.Text + "')";

        SqlDataAdapter sda = new SqlDataAdapter(query,con);
        sda.SelectCommand.ExecuteNonQuery();

        con.Close();

        MessageBox.Show("Inserted !!");
    }
}

谢谢

【问题讨论】:

  • 为什么要清除所有控件然后保存它们的(空)值?
  • 清除什么??不明白..你能解释一下吗
  • 停止一切,在阅读完所有有关 SQL 注入的知识之前不要编写任何代码。另外,不要以sa 连接。然后,您打开连接一次并在每次单击按钮时将其关闭 - 只需在每次单击按钮时打开它即可。最后,只需使用调试器查看发生了什么
  • @BurhanAhmed,在你的代码中你有:textBox1.Text = "";textBox2.Clear(); ...然后你存储它们的值(在查询中)
  • @LucasTrzesniewski 是的,我对 sql 注入知之甚少..但现在我只需要将我的数据插入数据库:P

标签: c# sql-server database winforms insert


【解决方案1】:

您的组合框中没有选择任何内容 - comboBox1.SelectedIndex = -1。但是您尝试将组合框文本插入到类别字段中。我会在那里寻找问题。

【讨论】:

    【解决方案2】:

    在插入数据库之前,您已经清除了文本框的值。 更改您的代码---

    {
        SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=phonemo;Persist Security Info=True;User ID=sa;Password=***********");
    
        public Phone()
        {
            InitializeComponent();
        }
    
        private void Phone_Load(object sender, EventArgs e)
        {
    
    
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
    
    
            DialogResult dialogResult = MessageBox.Show("This will Clear Text", "New", MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.Yes)
            {
                //do something
            }
            else if (dialogResult == DialogResult.No)
            {
                //do something else
            }
    
    
    
    
            con.Open();
            String query = "INSERT INTO phonemoo (Fname,Sname,Mobile,Email,Catagory) VALUES ('"+ textBox1.Text +"','"+ textBox2.Text +"','"+ textBox3.Text +"','"+ textBox4.Text +"','"+ comboBox1.Text + "')";
    
         SqlDataAdapter sda = new SqlDataAdapter(query,con);
         sda.SelectCommand.ExecuteNonQuery();
    
         con.Close();
    
            textBox1.Text = "";
            textBox2.Clear();
            textBox3.Text = "";
            textBox4.Clear();
            comboBox1.SelectedIndex = -1;
            textBox1.Focus();
    
         MessageBox.Show("Inserted !!");
        }
    }
    

    【讨论】:

    • 我应该做些什么改变??
    猜你喜欢
    • 2021-12-20
    • 2016-06-24
    • 2015-02-22
    • 2020-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-13
    • 1970-01-01
    相关资源
    最近更新 更多