如何在C#中创建注册表
本文关键字:创建 注册表 | 更新日期: 2023-09-27 18:22:07
注册表。
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);