为啥不能往这个sql语句里面传值啊!!!
做了一个查询页面,查询所有满足where的条件然后返回到GirdView上面,调试了下,发现值写不进去,代码如下:TrainInfo.TrainNo = int.Parse(txtTrainId.Text);
TrainInfo.FromPlace = txtFromPlace.Text;
TrainInfo.ToPlace = txtToPlace.Text;
TrainInfo.Type = int.Parse(txtType.Text);
string sql = "select * from Ticket where TrainNo=@trainNum and FromPlace=@fromPlace and ToPlace=@toPlace and Type=@type ";
SqlParameter[] sp = {
new SqlParameter("@trainNum",TrainInfo.TrainNo),
new SqlParameter ("@fromPlace",TrainInfo.FromPlace),
new SqlParameter("@toPlace",TrainInfo.ToPlace),
new SqlParameter("@type",TrainInfo.Type)
};
dataGridQuery.DataSource = DbHelper.GetDataSet(sql, sp);
public static DataTable GetDataSet(string sql, params SqlParameter[] values)--------------------编程问答-------------------- 断点调试
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
为什么 跟踪 哪里的值缺失
--------------------编程问答-------------------- 跟断点查。。。。 --------------------编程问答-------------------- new SqlParameter("@trainNum",TrainInfo.TrainNo)改成
new SqlParameter("trainNum",TrainInfo.TrainNo)
--------------------编程问答-------------------- foreach (SqlParameter para in cmdParms)
{
cmd.Parameters.Add(para);
}
--------------------编程问答--------------------
--------------------编程问答-------------------- 好像没问题。。断点看看。。 --------------------编程问答-------------------- cmd.Parameters.Add(sp); --------------------编程问答-------------------- TrainInfo.TrainNo = int.Parse(txtTrainId.Text);
给命令对象添加参数法:
例如:
int Id = 1;
string Name = "lui";
cmd.CommandText = "insert into TUserLogin values(@Id,@Name)";
//上条语句中直接在sql语句中写添加的参数名,不论参数类型都是如此.
SqlParameter para = new SqlParameter("@Id", SqlDbType.Int, 4);//生成一个名字为@Id的参数,必须以@开头表示是添加的参数,并设置其类型长度,类型长度与数据库中对应字段相同
para.Value = Id;//给参数赋值
cmd.Parameters.Add(para);//必须把参数变量添加到命令对象中去。
//以下类似
para = new SqlParameter("@Name", SqlDbType.VarChar, 16);
para.Value = Name;
com.Parameters.Add(para);
//然后就可以执行数据库操作了
TrainInfo.FromPlace = txtFromPlace.Text;
TrainInfo.ToPlace = txtToPlace.Text;
TrainInfo.Type = int.Parse(txtType.Text);
string sql = "select * from Ticket where TrainNo=@trainNum and FromPlace=@fromPlace and ToPlace=@toPlace and Type=@type ";
SqlParameter[] sp = {
new SqlParameter("@trainNum",SqlDbType.Int),
new SqlParameter ("@fromPlace",SqlDbType.VarChar),
new SqlParameter("@toPlace",SqlDbType.VarChar),
new SqlParameter("@type",SqlDbType.Int)
};
parameters[0].Value = TrainInfo.TrainNo;
parameters[1].Value = TrainInfo.FromPlace;
parameters[2].Value = txtToPlace.Text;
parameters[3].Value = TrainInfo.Type; --------------------编程问答-------------------- 代码看上去没问题,单步调试,进到DBHELP里看下,或者直接在sql监视器那里看下,输出地sql是什么 --------------------编程问答-------------------- 贴出的代码没看出有问题
dataGridQuery.DataBind();
是没贴出来 还是 ? --------------------编程问答--------------------
--------------------编程问答--------------------
public static DataTable GetDataSet(string sql, params SqlParameter[] values)
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(sql, Connection);
foreach (SqlParameter para in values)
{
cmd.Parameters.Add(para);
}
cmd.Parameters.AddRange(values);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
--------------------编程问答--------------------
public static DataTable GetDataSet(string sql, params SqlParameter[] values)
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(sql, Connection);
foreach (SqlParameter para in values)
{
cmd.Parameters.Add(para);
}
// cmd.Parameters.AddRange(values);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
成功了,按照这位大哥的改动了下,而且还忘记加databinding().... --------------------编程问答-------------------- 还有几个疑惑,像按照这位大哥的代码,也能成功:
SqlParameter[] sp = {
new SqlParameter("@trainNum",SqlDbType.Int),
new SqlParameter ("@fromPlace",SqlDbType.VarChar),
new SqlParameter("@toPlace",SqlDbType.VarChar),
new SqlParameter("@type",SqlDbType.Int)
};
parameters[0].Value = TrainInfo.TrainNo;
parameters[1].Value = TrainInfo.FromPlace;
parameters[2].Value = txtToPlace.Text;
parameters[3].Value = TrainInfo.Type;
不过使用这种方法很麻烦啊,如果有几十个值,依次那样赋值不是很麻烦。。。而且不太明白new SqlParameter("@trainNum",SqlDbType.Int),后面写那个类型有什么用 --------------------编程问答-------------------- parameters[0]=new SqlParameter("@trainNum",SqlDbType.Int),
parameters[0].Value = TrainInfo.TrainNo;
先定义一个,后赋值
后面写那个类型是在DB定义的类型
补充:.NET技术 , ASP.NET