string userpassword = txtpassword.Text;
string userid = txtid.Text;
string useremail = txtemail.Text;//以上是读取文本框中输入的字符
SqlConnection conn = new SqlConnection(@"Data Source=LENOVO-PC;Initial Catalog=zhuce;Persist Security Info=True;User ID=sa;Password=xxxx"); //连接数据库
ps:连接数据库是注意登录机名!还有注意是混合登录还是windows登录
SqlConnection conn = new SqlConnection(@"data source=LENOVO-PC;database=master;integrated security=true"); //这为windows登录验证
string strsql = "insert into T_Zhuce(username,userpassword,userid,useremail) values(@username,@userpassword,@userid,@useremail)";//此为拼接sql数据
SqlCommand cmd = new SqlCommand();//像数据库发送这条sql语句
cmd.Connection = conn;//指定SqlComand对象要向哪个数据库连接发送sql语句
cmd.CommandText = strsql;
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@userpassword", userpassword);
cmd.Parameters.AddWithValue("@userid", userid);
cmd.Parameters.AddWithValue("@useremail", useremail);//绿色为向数据库发送sql语句,这行代码才是真正的发送sql语句的动作。
cmd.ExecuteNonQuery();
conn.Close();//关闭数据库。
Response.Write("注册成功");
SqlConnection conn = new SqlConnection(@"data source=LENOVO-PC;database=master;integrated security=true");
conn.Open();
string strsql = "insert into T_Name(UserName,UserPwd,UserEmail,RealName)values('" + Username + "','" + pwd + "','" + Email + "','" + realname + "')";
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
conn.Close();
Response.Write("注册成功");这也是一种写入数据库的方法但是不常用同上面的区别是在拼接字符串时此种方法易破解sql注入。。所以见解还是多用上面的方法写入数据库。
很多天没写啦!把老师讲的一点小内容和大家分享下!!!