sql参数化构造问题!

j_hunter 2009-07-21 09:56:56
string picname = this.picname.Text.ToString();
string picdre = this.picdre.Text.ToString();
string pic = this.PicUpLoads1.StrPicPath.ToString();


string sqlstr = "insert into DiyPic(Picname,Picdre,Pic)values(@Picname,@Picdre,@Pic)";
using (SqlConnection con = SQLcs.CreateCon())
{
SqlParameter Picname;
SqlParameter Picdre;
SqlParameter Pic;
SqlCommand cmd = new SqlCommand(sqlstr, con);
Picname = new SqlParameter();
Picname.ParameterName = "@Picname";
Picname.SqlDbType = SqlDbType.NVarChar;
Picname.Size = 100;
Picname.Value = picname;
Picdre = new SqlParameter();
Picdre.ParameterName = "@Picdre";
Picdre.SqlDbType = SqlDbType.NVarChar;
Picdre.Size = 100;
Picdre.Value = picdre;
Pic = new SqlParameter();
Pic.ParameterName = "@Pic";
Pic.SqlDbType = SqlDbType.NVarChar;
Pic.Size = 100;
Pic.Value = pic;




//cmd.Parameters.Add(@Picname, SqlDbType.NVarChar, 100).Value = picname;
//cmd.Parameters.Add(@Picdre, SqlDbType.NVarChar, 100).Value = picdre;
//cmd.Parameters.Add(@Pic, SqlDbType.NVarChar, 100).Value = pic;
con.Open();
sqlda.SelectCommand = cmd;
sqlda.Fill(ds);


为什么我运行到 sqlda.Fill(ds);这一步说是:必须声明变量 '@Picname'。 我改变了好几种写法了 还是这样 大家看下是什么原因啊?
...全文
79 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
fanglong168 2009-07-21
  • 打赏
  • 举报
回复
j_hunter 2009-07-21
  • 打赏
  • 举报
回复
不好意思 刚刚看错了 问题解决了 还是太粗心。 散分了
j_hunter 2009-07-21
  • 打赏
  • 举报
回复
还是你两眼睛尖,但是加了还是提示:当前上下文中不存在名称“Picname”;?第一次用参数,不熟。
lchy110 2009-07-21
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ljhcy99 的回复:]
string Picname = this.picname.Text.ToString();
        string Picdre = this.picdre.Text.ToString();
        string Pic = this.PicUpLoads1.StrPicPath.ToString();

        string sqlstr = "insert into DiyPic(Picname,Picdre,Pic) values(@Picname,@Picdre,@Pic)";
        using (SqlConnection con = SQLcs.CreateCon())
        {
            SqlCommand cmd = new SqlCommand(sqlstr, con);
            cmd.Parameters.Add("@Picname", SqlDbType.NVarChar, 100).Value = Picname;
            cmd.Parameters.Add("@Picdre", SqlDbType.NVarChar, 100).Value = Picdre;
            cmd.Parameters.Add("@Pic", SqlDbType.NVarChar, 100).Value = Pic;
            con.Open();
            sqlda.SelectCommand = cmd;
            sqlda.Fill(ds);
        }
参数名字要套一个引号
[/Quote]
正解 楼主没假双引号 编译的时候编译器 把@Picname当成变量处理 当然会提示没定义
ljhcy99 2009-07-21
  • 打赏
  • 举报
回复
string Picname = this.picname.Text.ToString();
string Picdre = this.picdre.Text.ToString();
string Pic = this.PicUpLoads1.StrPicPath.ToString();

string sqlstr = "insert into DiyPic(Picname,Picdre,Pic) values(@Picname,@Picdre,@Pic)";
using (SqlConnection con = SQLcs.CreateCon())
{
SqlCommand cmd = new SqlCommand(sqlstr, con);
cmd.Parameters.Add("@Picname", SqlDbType.NVarChar, 100).Value = Picname;
cmd.Parameters.Add("@Picdre", SqlDbType.NVarChar, 100).Value = Picdre;
cmd.Parameters.Add("@Pic", SqlDbType.NVarChar, 100).Value = Pic;
con.Open();
sqlda.SelectCommand = cmd;
sqlda.Fill(ds);
}
参数名字要套一个引号
j_hunter 2009-07-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 j_hunter 的回复:]
呵呵 可能大家都看花了
那我就把简便的那种写出来了
        string Picname = this.picname.Text.ToString();
        string Picdre = this.picdre.Text.ToString();
        string Pic = this.PicUpLoads1.StrPicPath.ToString();

        string sqlstr = "insert into DiyPic(Picname,Picdre,Pic) values(@Picname,@Picdre,@Pic)";
        using (SqlConnection con = SQLcs.CreateCon())
        {
            SqlCommand cmd = new SqlCommand(sqlstr, con);
            cmd.Parameters.Add(@Picname, SqlDbType.NVarChar, 100).Value = Picname;
            cmd.Parameters.Add(@Picdre, SqlDbType.NVarChar, 100).Value = Picdre;
            cmd.Parameters.Add(@Pic, SqlDbType.NVarChar, 100).Value = Pic;
            con.Open();
            sqlda.SelectCommand = cmd;
            sqlda.Fill(ds);
        }

但提示的错误还是一样的  运行到 sqlda.Fill(ds);这一步说是:第 1 行: '/' 附近有语法错误。
必须声明变量 '@Picname'。
[/Quote]
是不是我直接插入物理地址引起的/CDV/fdsfdsf.gif 像这样的地址名怎么转换后再插入?
蓝海D鱼 2009-07-21
  • 打赏
  • 举报
回复
up
j_hunter 2009-07-21
  • 打赏
  • 举报
回复
呵呵 可能大家都看花了
那我就把简便的那种写出来了
string Picname = this.picname.Text.ToString();
string Picdre = this.picdre.Text.ToString();
string Pic = this.PicUpLoads1.StrPicPath.ToString();

string sqlstr = "insert into DiyPic(Picname,Picdre,Pic) values(@Picname,@Picdre,@Pic)";
using (SqlConnection con = SQLcs.CreateCon())
{
SqlCommand cmd = new SqlCommand(sqlstr, con);
cmd.Parameters.Add(@Picname, SqlDbType.NVarChar, 100).Value = Picname;
cmd.Parameters.Add(@Picdre, SqlDbType.NVarChar, 100).Value = Picdre;
cmd.Parameters.Add(@Pic, SqlDbType.NVarChar, 100).Value = Pic;
con.Open();
sqlda.SelectCommand = cmd;
sqlda.Fill(ds);
}

但提示的错误还是一样的 运行到 sqlda.Fill(ds);这一步说是:必须声明变量 '@Picname'。
j_hunter 2009-07-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hdhai9451 的回复:]
你这样传递参数太难了,一般是经过实体传递参数的

例如:
public int AddorUpdate(Employee employee, string oprt_mode)
{
    SqlParameter[] sp = {
        new SqlParameter("@oprt_mode",oprt_mode),
        new SqlParameter("@employee_no",employee.employee_no),
        new SqlParameter("@employee_name",employee.employee_name),
        new SqlParameter("@password",employee.password),
        new SqlParameter("@sex",employee.sex),
        new SqlParameter("@birthday",employee.birthday),
        new SqlParameter("@tel",employee.tel),
        new SqlParameter("@email",employee.email),
        new SqlParameter("@addr",employee.addr),
        new SqlParameter("@remark",employee.remark),
        new SqlParameter("@oprt_man",employee.oprt_man)
    };
    return opt.RunSql("proc_tbemployee_oprt", sp);
}

[/Quote]
你这种写法是不错,但是我这是在老系统上加东西,都是编译过的用不了实体那样传,下面这种方法应该不难吧,但还是出错。 是不是我传递物理路径名的问题,string pic = this.PicUpLoads1.StrPicPath.ToString(); Pic传递的是服务器的一个目录地址,如asb/222.gif,这样直接传对不?


cmd.Parameters.Add(@Picname, SqlDbType.NVarChar, 100).Value = picname;
cmd.Parameters.Add(@Picdre, SqlDbType.NVarChar, 100).Value = picdre;
cmd.Parameters.Add(@Pic, SqlDbType.NVarChar, 100).Value = pic;
fengjian_428 2009-07-21
  • 打赏
  • 举报
回复
写法太麻烦
Andy__Huang 2009-07-21
  • 打赏
  • 举报
回复
你这样传递参数太难了,一般是经过实体传递参数的

例如:
public int AddorUpdate(Employee employee, string oprt_mode)
{
SqlParameter[] sp = {
new SqlParameter("@oprt_mode",oprt_mode),
new SqlParameter("@employee_no",employee.employee_no),
new SqlParameter("@employee_name",employee.employee_name),
new SqlParameter("@password",employee.password),
new SqlParameter("@sex",employee.sex),
new SqlParameter("@birthday",employee.birthday),
new SqlParameter("@tel",employee.tel),
new SqlParameter("@email",employee.email),
new SqlParameter("@addr",employee.addr),
new SqlParameter("@remark",employee.remark),
new SqlParameter("@oprt_man",employee.oprt_man)
};
return opt.RunSql("proc_tbemployee_oprt", sp);
}

62,039

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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