关于执行 SqlCommand com = new SqlCommand(sql,con);的问题

立志成为小胖纸的男人 2014-06-02 09:57:15
[
  SqlConnection con = new SqlConnection("server=AIXUE-PC\\WUSHENGQI;database=sushe;uid=sa;pwd=1234");
con.Open();
string slt = "select count(*) from name_Table where Password=@password and User=@user";
SqlCommand cmd = new SqlCommand(slt, con);
cmd.Parameters.Add(new SqlParameter("@user",SqlDbType.Char,20));
cmd.Parameters["@user"].Value = TextBox1.Text;
cmd.Parameters.Add(new SqlParameter("@password", SqlDbType.Char, 20));
cmd.Parameters["@password"].Value = TextBox2.Text;
if (Convert.ToInt32(cmd.ExecuteScalar()) >0)
{
Response.Redirect("../manage/manage.aspx");
}
else
{
ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('用户名或密码错误')</script>");
}


这个是我写的代码,问过很同学。他们都说语法没有错误,可是就是读不出数据库里面的数据。Convert.ToInt32(cmd.ExecuteScalar()) 的结果是零
...全文
441 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 4 楼 sp1234 的回复:
SqlDbType.Char,20 这是啥啊?你在SQL Server客户端工具的查询分析器重(或者vs服务器工具的SQL Server查询分析器中)写一个sql语句,并且字符串常量是20个字符的定长字符串,看看查询出几行结果。
已经搞定,谢谢大神啦
  • 打赏
  • 举报
回复
引用 8 楼 AC1992 的回复:
引用 7 楼 mlxwl2013 的回复:
程序里面也要改哦。
数据库和程序同步都改了,依然木有用!
前辈说的是一个问题,还有一个问题就是用了User关键字,现在可以了!
  • 打赏
  • 举报
回复
引用 7 楼 mlxwl2013 的回复:
程序里面也要改哦。
数据库和程序同步都改了,依然木有用!
mlxwl2013 2014-06-02
  • 打赏
  • 举报
回复
程序里面也要改哦。
  • 打赏
  • 举报
回复
引用 2 楼 mlxwl2013 的回复:
我估计你数据库里相关字段设成了char型,char是定长,不足位会自动前面补空格,自然不会相等了。数据库相应字段应该改成varchar型。
改过来了,还是没有用
mlxwl2013 2014-06-02
  • 打赏
  • 举报
回复
一般用户名字段用nvarchar,密码字段用varchar
  • 打赏
  • 举报
回复
SqlDbType.Char,20 这是啥啊?你在SQL Server客户端工具的查询分析器重(或者vs服务器工具的SQL Server查询分析器中)写一个sql语句,并且字符串常量是20个字符的定长字符串,看看查询出几行结果。
  • 打赏
  • 举报
回复
会不会是SqlCommand com = new SqlCommand(sql,con);这个地方的问题
mlxwl2013 2014-06-02
  • 打赏
  • 举报
回复
我估计你数据库里相关字段设成了char型,char是定长,不足位会自动前面补空格,自然不会相等了。数据库相应字段应该改成varchar型。
  • 打赏
  • 举报
回复
server=AIXUE-PC\\WUSHENGQI;database=sushe;uid=sa;pwd=1234 连接数据库语句 select count(*) from name_Table where Password=@password and User=@user 执行查询操作 TextBox1.Text 用户名 TextBox2.Text 密码 数据库中用户名是s1001,密码是111 然后我输入是1001,和111 结果影响的行数为0 是为什么呢?
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace 随机点名器 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } string a = ""; private void Form2_Load(object sender, EventArgs e) { // TODO: 这行代码将数据加载到表“dataSet1.学生信息”中。您可以根据需要移动或删除它。 // this.学生信息TableAdapter.Fill(this.dataSet1.学生信息); } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } private void button1_Click(object sender, EventArgs e) { String constr = "Server = 10.100.102.3;user id = dotnet;pwd = 123;database = wgz"; //String constr = @"data source = .;user id = ;pwd = ;database = TEST"; SqlConnection con = new SqlConnection(); con.ConnectionString = constr; con.Open(); // string dianming = "SELECT top 1 [学号] FROM [学生信息] ORDER BY NEWID()"; string sql = "SELECT top 1 * FROM [学生信息] ORDER BY NEWID()"; SqlDataAdapter da = new SqlDataAdapter(sql, con); DataSet ds = new DataSet(); da.Fill(ds, "cs"); dataGridView1.DataSource = ds.Tables["cs"]; a = ds.Tables[0].Rows[0][1].ToString(); con.Close(); } private void button2_Click(object sender, EventArgs e) { string b = a; String constr = "Server = 10.100.102.3;user id = dotnet;pwd = 123;database = wgz"; SqlConnection con = new SqlConnection(); con.ConnectionString = constr; con.Open(); string chuqin = "update [学生信息] set [出勤]=[出勤]+1 where [学号]='"+b+"'"; SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = chuqin; int i1 = cmd.ExecuteNonQuery(); string chuli = "update [学生信息] set [平时成绩] = [平时成绩] + 3 where [学号]='" + b + "'"; SqlCommand com = new SqlCommand(); com.Connection = con; com.CommandText = chuli; int i2 = com.ExecuteNonQuery(); string sql = "SELECT * FROM [学生信息] where [学号]='" + b + "'"; SqlDataAdapter da = new SqlDataAdapter(sql, con); DataSet ds = new DataSet(); da.Fill(ds, "cs"); dataGridView1.DataSource = ds.Tables["cs"]; con.Close(); } private void button3_Click(object sender, EventArgs e) { string b = a; String constr = "Server = 10.100.102.3;user id = dotnet;pwd = 123;database = wgz"; SqlConnection con = new SqlConnection(); con.ConnectionString = constr; con.Open(); string kuangke = "update [学生信息] set [旷课]=[旷课]+1 where [学号]='" + b + "'"; SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = kuangke; int i1 = cmd.ExecuteNonQuery(); string chuli = "update [学生信息] set [平时成绩] = [平时成绩] - 5 where [学号]='" + b + "'"; SqlCommand com = new SqlCommand(); com.Connection = con; com.CommandText = chuli; int i2 = com.ExecuteNonQuery(); string sql = "SELECT * FROM [学生信息] where [学号]='" + b + "'"; SqlDataAdapter da = new SqlDataAdapter(sql, con); DataSet ds = new DataSet(); da.Fill(ds, "cs"); dataGridView1.DataSource = ds.Tables["cs"]; con.Close(); } private void button4_Click(object sender, EventArgs e) { string b = a; String constr = "Server = 10.100.102.3;user id = dotnet;pwd = 123;database = wgz"; SqlConnection con = new SqlConnection(); con.ConnectionString = constr; con.Open(); string qingjia = "update [学生信息] set [请假]=[请假]+1 where [学号]='" + b + "'"; SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = qingjia; int i1 = cmd.ExecuteNonQuery(); string chuli = "update [学生信息] set [平时成绩] = [平时成绩] - 1 where [学号]='" + b + "'"; SqlCommand com = new SqlCommand(); com.Connection = con; com.CommandText = chuli; int i2 = com.ExecuteNonQuery(); string sql = "SELECT * FROM [学生信息] where [学号]='" + b + "'"; SqlDataAdapter da = new SqlDataAdapter(sql, con); DataSet ds = new DataSet(); da.Fill(ds, "cs"); dataGridView1.DataSource = ds.Tables["cs"]; con.Close(); } private void button5_Click(object sender, EventArgs e) { String constr = "Server = 10.100.102.3;user id = dotnet;pwd = 123;database = wgz"; SqlConnection con = new SqlConnection(); con.ConnectionString = constr; con.Open(); string sql = "SELECT * FROM [学生信息] ORDER BY [学号]"; SqlDataAdapter da = new SqlDataAdapter(sql, con); DataSet ds = new DataSet(); da.Fill(ds, "cs"); dataGridView1.DataSource = ds.Tables["cs"]; con.Close(); } private void button7_Click(object sender, EventArgs e) { Form1 form = new Form1(); form.Show(); this.Hide(); } } } 基于C# C/S的随机点名器

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧