c# 必须声明标量变量@ 可是已经声明了啊

sinat_24853299 2015-12-11 09:42:22
string sql = "insert into commodity values(@photo,@number, @name,@ pwd, @mail,@sex,@call, @college,@major)";
SqlCommand com = new SqlCommand(sql1, DBhelper.con);
// com.Parameters.Add("@photo", SqlDbType.Image).Value =file ;
com.Parameters.AddWithValue("@photo", file);
com.Parameters.AddWithValue("@number", number);
com.Parameters.AddWithValue("@name", name);
com.Parameters.AddWithValue("@pwd", pwd);
com.Parameters.AddWithValue("@mail", mail);
com.Parameters.AddWithValue("@sex", sex);
com.Parameters.AddWithValue("@call", call);
com.Parameters.AddWithValue("@college", college);
com.Parameters.AddWithValue("@major", major);
...全文
1200 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
万物皆可搜 2018-09-30
  • 打赏
  • 举报
回复
哪里声明变量?
江南小鱼 2015-12-12
  • 打赏
  • 举报
回复
自增列,不要试图插入值。
  • 打赏
  • 举报
回复
你的number是自增列,新增sql去掉,当然同步的你要将你的insert sql进行调整,改成指定列名的方式
sinat_24853299 2015-12-11
  • 打赏
  • 举报
回复
引用 3 楼 starfd 的回复:
@ pwd, @mail,
这么大一个空格

这个问题解决了 又出一个。。
  • 打赏
  • 举报
回复
@ pwd, @mail, 这么大一个空格
sinat_24853299 2015-12-11
  • 打赏
  • 举报
回复
upupup
sinat_24853299 2015-12-11
  • 打赏
  • 举报
回复
所有代码
 
 string number = txt_number.Text;
              string name = txt_name.Text;
              string mail = txt_mail.Text;
              string pwd = txt_pwd.Text;
              string sex = Convert.ToString("");
            if (rb_boy.Checked)
            { sex = "男"; }
            else
            { sex = "女"; }

           
              string college = cmb_college.Text;
              string major = cmb_major.Text;
              string call = txt_call.Text;


            string filepath = txt_filename.Text.Trim();
            FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
            byte[] file = new byte[fs.Length];
            fs.Read(file, 0, (int)fs.Length);
            fs.Close();


             // string sql = string.Format("insert into customer values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}',@photo)", number, name, pwd, mail, sex, call, college, major);
     
            string sql1 = string.Format("select count(*)  from customer where number={0}",txt_number.Text);
            string sql = "insert into commodity values(@photo,@number, @name,@ pwd, @mail,@sex,@call, @college,@major)";
             SqlCommand com = new SqlCommand(sql1, DBhelper.con);
            //   com.Parameters.Add("@photo", SqlDbType.Image).Value =file ;
            com.Parameters.AddWithValue("@photo", file);
            com.Parameters.AddWithValue("@number", number);
            com.Parameters.AddWithValue("@name", name);
            com.Parameters.AddWithValue("@pwd", pwd);
            com.Parameters.AddWithValue("@mail", mail);
            com.Parameters.AddWithValue("@sex", sex);
            com.Parameters.AddWithValue("@call", call);
            com.Parameters.AddWithValue("@college", college);
            com.Parameters.AddWithValue("@major", major);
           /* com.Parameters.Add("@number", SqlDbType.Text).Value = Convert.ToInt32(txt_number.Text);
            com.Parameters.Add("@name", SqlDbType.Text).Value = Convert.ToInt32(txt_name.Text);
            com.Parameters.Add("@pwd", SqlDbType.Text).Value = txt_pwd.Text;
            com.Parameters.Add("@mail", SqlDbType.Text).Value = txt_mail.Text;
            com.Parameters.Add("@call", SqlDbType.Text).Value = txt_call.Text;
            com.Parameters.Add("@college", SqlDbType.Text).Value = cmb_college.Text;
            com.Parameters.Add("@major", SqlDbType.Text).Value = cmb_major.Text;
            if (rb_boy.Checked == true)
            { com.Parameters.Add("@sex", SqlDbType.Text).Value = "男"; }
            else
            { com.Parameters.Add("@sex", SqlDbType.Text).Value = "女"; }
            */


            // 获取图片地址


            if (txt_mail.Text.Length == 0 || txt_name.Text.Length == 0 || txt_number.Text.Length == 0 ||
                    txt_pwd.Text.Length == 0 || rb_boy.Checked == false && rb_girl.Checked == false ||
                    cmb_college.Text.Length == 0 || cmb_major.Text.Length == 0 || txt_call.Text.Length == 0)
            {
                MessageBox.Show("信息不完整!");
            }
            else
                try
                {
                    DBhelper.con.Open();
                    int i = (Int32)com.ExecuteScalar();
                    if (i > 0)                              //判断该学号是否注册过
                    {
                        MessageBox.Show("此学号已存在!");
                        txt_number.Text = "";
                        txt_number.Focus();
                    }
                    else
                    {
                        com.CommandText = sql;
                        i = com.ExecuteNonQuery();
                        MessageBox.Show("添加学生信息成功!");
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                { DBhelper.con.Close(); }

110,533

社区成员

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

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

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