请高手帮我看看这段连接数据库代码有什么错误?
程序代码:
string userName = this.txt_username.Text; string userPwd = this.txt_pwd.Text; string userSex = this.txt_sex.Text; int userAge = Convert.ToInt32(this.txt_age.Text); int count; SqlConnection con = new SqlConnection("server=localhost;uid=sa;pwd=;database=textdatabase"); con.Open(); SqlCommand cmd = new SqlCommand(); = "select count(*) from TUserInfo where '" + userName + "'=UserName"; count = Convert.ToInt32(cmd.ExecuteScalar()); if (count > 0) { MessageBox.Show("该用户已经存在,请重新输入用户名。"); this.txt_username.Text = ""; this.txt_pwd.Text = ""; this.txt_sex.Text = ""; this.txt_age.Text = ""; } else { SqlDataAdapter sda = new SqlDataAdapter("select UserName,UserPwd from TUserInfo", con); SqlCommandBuilder scb = new SqlCommandBuilder(sda); DataSet ds = new DataSet(); sda.Fill(ds, "TUsername"); DataRow dr = ds.Tables["TUserInfo"].NewRow(); dr["UserName"] = userName; dr["UserPwd"] = userPwd; dr["UserSex"] = userSex; dr["UserAge"] = userAge; ds.Tables["TUserInfo"].Rows.Add(dr); sda.Update(ds,"TUserInfo"); con.Close(); }
是winForm程序,这段代码是写在一个添加按钮里面的
[[it] 本帖最后由 Kiss_零 于 2008-11-21 18:23 编辑 [/it]]