SQL Insert Query Using C#

后端 未结 8 1781
长情又很酷
长情又很酷 2020-11-27 17:24

I\'m having an issue at the moment which I am trying to fix. I just tried to access a database and insert some values with the help of C#

The things I tried (worked)

8条回答
  •  醉话见心
    2020-11-27 17:56

    static SqlConnection myConnection;
    
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            myConnection = new SqlConnection("server=localhost;" +
                                                          "Trusted_Connection=true;" +
                 "database=zxc; " +
                                                          "connection timeout=30");
            try
            {
    
                myConnection.Open();
                label1.Text = "connect successful";
    
            }
            catch (SqlException ex)
            {
                label1.Text = "connect fail";
                MessageBox.Show(ex.Message);
            }
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
    
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            String st = "INSERT INTO supplier(supplier_id, supplier_name)VALUES(" + textBox1.Text + ", " + textBox2.Text + ")";
            SqlCommand sqlcom = new SqlCommand(st, myConnection);
            try
            {
                sqlcom.ExecuteNonQuery();
                MessageBox.Show("insert successful");
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    

提交回复
热议问题