如何在C#中创建注册表?

时间:2015-11-17 03:40:31

标签: c# mysql

registration form.

USN-主键 密码和联系电话 - 浮动 其余的是varchar 如何防止我的代码出错?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Portal_Exam1
{
    public partial class Register : Form
    {
        public Register()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=ADMIN-\MSSQLSERVERR;Initial Catalog=Register;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("insert into Student values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "')", con);
            con.Open();
            int i = cmd.ExecuteNonQuery();


     con.Close();
        if (i > 0)
        {
            MessageBox.Show("Data inserted successfully!");
        }
        else
        {
            MessageBox.Show("There is some problem");
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.Hide();
        Admin_Panel aa = new Admin_Panel();
        aa.Show();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        textBox1.Text = "";
        textBox2.Text = "";
        textBox3.Text = "";
        textBox4.Text = "";
        textBox5.Text = "";
        textBox6.Text = "";
        textBox7.Text = "";
    }
}

}

error

1 个答案:

答案 0 :(得分:0)

在查询中插入列名称

"Sqlcommand cmd = New Sqlcommand("INSERT INTO tablename (column_name) VALUES ('" + textbox1.text + "')",con);

如果使用参数化查询

,也会更好
"Sqlcommand cmd = New Sqlcommand("INSERT INTO tablename (column_name) VALUES (@text_box)",con);
cmd.Parameters.AddWithValue("@text_box", textBox1.text);
相关问题