问题描述
namespace学生信息管理系统{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbtn1_Click(objectsender,EventArgse){stringtbx1=textBox1.Text.Trim();stringtbx2=textBox2.Text.Trim();using(SqlConnectionconn=newSqlConnection(@"DataSource=.SQLEXPRESS;AttachDBFilename=|Datadirectory|Database1.mdf;IntegratedSecurity=true;UserInstance=true"))//连接Sqlserver数据库{conn.Open();using(SqlCommandcmd=conn.CreateCommand()){cmd.CommandText="select*fromT_studentwherename='"+tbx1+"'";using(SqlDataReaderreader=cmd.ExecuteReader()){if(reader.Read()){//用户名存在stringdbpassword=reader.GetString(reader.GetOrdinal("password")).Trim();if(tbx2==dbpassword){MessageBox.Show("登录成功!");}else{MessageBox.Show("用户名存在,密码错误!");}}else{MessageBox.Show("用户名不存在!","提示");}}}conn.Close();MessageBox.Show("OK","提示");//Console.ReadKey();}}privatevoidbtn2_Click(objectsender,EventArgse){stringtbx1=textBox1.Text.Trim();stringtbx2=textBox2.Text.Trim();using(SqlConnectionconn=newSqlConnection(@"DataSource=.SQLEXPRESS;AttachDBFilename=|Datadirectory|Database1.mdf;IntegratedSecurity=true;UserInstance=true"))//连接Sqlserver数据库{conn.Open();using(SqlCommandcmd=conn.CreateCommand()){cmd.CommandText="insertintoT_student(name,password)values('@name','@password')";cmd.Parameters.AddWithValue("@name",tbx1);cmd.Parameters.AddWithValue("@password",tbx2);cmd.ExecuteNonQuery();MessageBox.Show("注册成功!");}conn.Close();MessageBox.Show("OK","提示");}}}}staticclassProgram{///<summary>///应用程序的主入口点。///</summary>[STAThread]staticvoidMain(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(newForm1());stringdataDir=AppDomain.CurrentDomain.BaseDirectory;if(dataDir.EndsWith(@"binDebug")||dataDir.EndsWith(@"binRelease")){dataDir=System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;AppDomain.CurrentDomain.SetData("DataDirectory",dataDir);}}}登录成功,但是不能注册!
解决方案
解决方案二:
introw=cmd.ExecuteNonQuery();//单步调试下,看row是否大于0,等于0代表插入失败
解决方案三:
cmd.CommandText="insertintoT_student(name,password)values(@name,@password)";要写成这样