繁体   English   中英

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

[英]how to create Registration Form in C#?

报名表格。

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 = "";
    }
}

}

错误

在查询中插入列名

"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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM