C#连接数据库注册显示未初始化,代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
//先获取页面信息
string st_username = this.TextBox1.Text;
string st_password = this.TextBox2.Text;
string st_number = this.TextBox3.Text;
string st_name = this.TextBox4.Text;
string st_department = this.TextBox5.Text;
string st_class = this.TextBox6.Text;
//获得连接数据库字符串
string conn = "Data Source=PC-20140706QNKM;Initial Catalog=Management;Integrated Security=True";
//创建sqlconnection对像
SqlConnection connection = new SqlConnection(conn);
//打开数据库
connection.Open();
//创建一个数据库命令行
SqlCommand Command = new SqlCommand(conn);
Command.CommandText = "insert into student(st_username,st_password,st_number,st_name,st_department,st_class)values(@st_username,@st_password,@st_number,@st_name,@st_department,@st_class)";
Command.Parameters.AddWithValue("@st_username", TextBox1);
Command.Parameters.AddWithValue("@st_password", TextBox2);
Command.Parameters.AddWithValue("@st_number", TextBox3);
Command.Parameters.AddWithValue("@st_name", TextBox4);
Command.Parameters.AddWithValue("@st_department", TextBox5);
Command.Parameters.AddWithValue("@st_class", TextBox6);
//执行数据库命令
Command.ExecuteNonQuery();
//提示注册成功
Label1.Text = "注册成功";
//清空输入框
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
TextBox6.Text = "";
//关闭命令和链接
Command.Dispose();
}
|