问题描述
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Data.SqlClient;usingSystem.Configuration;usingSystem.IO;namespaceyonghudenglu{publicpartialclassfrmEdit:Form{publicfrmEdit(){InitializeComponent();}privatestringstrConn=ConfigurationManager.ConnectionStrings["yonghudenglu.Properties.Settings.TongXunGuanLiConnectionString"].ToString();publicstringtxtFilePath="";privateintgroupCount=0;privatestringstrUserName="";privateintcontentCount=0;publicbyte[]picData;privatevoidbtnCancel_Click(objectsender,EventArgse){DialogResult=DialogResult.Cancel;}privateintID;publicfrmEdit(intid){InitializeComponent();ID=id;}privatevoidpic_Click(objectsender,EventArgse){if(dlg.ShowDialog()==DialogResult.OK){pic.Image=Image.FromFile(dlg.FileName);txtFilePath=dlg.FileName;}}privatevoidbtnOK_Click(objectsender,EventArgse){stringname;if(txtName.Text==""){MessageBox.Show("联系人姓名不能为空!","添加失败",MessageBoxButtons.OK,MessageBoxIcon.Error);return;}else{name=txtName.Text;}stringgroup;if(cmbGroup.Text.Trim()==""){group="未分组";}else{group=cmbGroup.Text.Trim();}stringphone=txtPhone.Text.Trim();stringcompany=txtCompany.Text.Trim();stringemail=txtEmail.Text.Trim();byte[]b=null;if(txtFilePath!=""){try{FileStreamfs=newFileStream(txtFilePath,FileMode.Open,FileAccess.Read);intlen=Convert.ToInt32(fs.Length);b=newbyte[len];fs.Read(b,0,len);fs.Close();}catch{b=null;}}else{b=picData;}try{SqlConnectionconn=newSqlConnection(strConn);conn.Open();SqlCommandcmd_update=newSqlCommand();cmd_update.Connection=conn;cmd_update.CommandText="updateLianXiRensetZuBie=@ZuBie,XingMing=@XingMing,GongZuoDanWei=@GongZuoDanWei,LianXiDianHua=@LianXiDianHua,DianZiYouXiang=@DianZiYouXiang,ZhaoPian=@ZhaoPian";cmd_update.CommandText+="whereBianHao=@BianHao";cmd_update.Parameters.Add("@BianHao",SqlDbType.Int);cmd_update.Parameters.Add("@ZuBie",SqlDbType.VarChar,50);cmd_update.Parameters.Add("@XingMing",SqlDbType.VarChar,50);cmd_update.Parameters.Add("@GongZuoDanWei",SqlDbType.VarChar,500);cmd_update.Parameters.Add("@LianXiDianHua",SqlDbType.VarChar,200);cmd_update.Parameters.Add("@DianZiYouXiang",SqlDbType.VarChar,200);cmd_update.Parameters.Add("@照片",SqlDbType.Image);cmd_update.Parameters["@BianHao"].Value=ID;cmd_update.Parameters["@ZuBie"].Value=group;cmd_update.Parameters["@XingMing"].Value=name;cmd_update.Parameters["@GongZuoDanWei"].Value=company;cmd_update.Parameters["@LianXiDianHua"].Value=phone;cmd_update.Parameters["@DianZiYouXiang"].Value=email;if(txtFilePath==""&&picData==null){cmd_update.Parameters["@ZhaoPian"].Value=DBNull.Value;}else{cmd_update.Parameters["@ZhaoPian"].Value=b;}cmd_update.ExecuteNonQuery();conn.Close();DialogResult=DialogResult.OK;}catch(Exceptionee){MessageBox.Show(ee.Message,"添加失败",MessageBoxButtons.OK,MessageBoxIcon.Error);return;}}protectedvoidLoadGroup(){try{cmbGroup.Items.Clear();SqlConnectionconn=newSqlConnection(strConn);SqlCommandcmd=newSqlCommand();cmd.Connection=conn;cmd.CommandText="selectdistinctZuBiefromLianXiRen";conn.Open();SqlDataReadersdr=cmd.ExecuteReader();while(sdr.Read()){cmbGroup.Items.Add(sdr[0].ToString());}sdr.Close();conn.Close();if(groupCount>0&&cmbGroup.Items.Count==groupCount){cmbGroup.DropDownStyle=ComboBoxStyle.DropDownList;}else{cmbGroup.DropDownStyle=ComboBoxStyle.DropDown;}}catch{}}privatevoidfrmEdit_Load(objectsender,EventArgse){strUserName=((frmMain)(this.Owner)).tsslUserName.Text;LoadGroup();SqlConnectionconn=newSqlConnection(strConn);SqlCommandcmd=newSqlCommand();cmd.Connection=conn;cmd.CommandText="select*fromLianXiRenwhereBianHao="+ID.ToString();SqlDataAdaptersda=newSqlDataAdapter(cmd);DataSetds=newDataSet();sda.Fill(ds);if(ds.Tables[0].Rows.Count==1){txtName.Text=ds.Tables[0].Rows[0]["XingMing"].ToString();cmbGroup.Text=ds.Tables[0].Rows[0]["ZuBie"].ToString();txtPhone.Text=ds.Tables[0].Rows[0]["LianXiDianHua"].ToString();txtCompany.Text=ds.Tables[0].Rows[0]["GongZuoDanWei"].ToString();txtEmail.Text=ds.Tables[0].Rows[0]["DianZiYouXiang"].ToString();if(ds.Tables[0].Rows[0]["ZhaoPian"]==DBNull.Value){pic.Image=yonghudenglu.Properties.Resources.png_0010;}else{byte[]b=(byte[])(ds.Tables[0].Rows[0]["ZhaoPian"]);pic.Image=Image.FromStream(newMemoryStream(b));picData=b;}}cmd.CommandText="select*fromYongHuwhereYongHuMing='"+strUserName+"'";conn.Open();SqlDataReadersdr=cmd.ExecuteReader();if(sdr.Read()){groupCount=Convert.ToInt32(sdr["ZuBieSLZDZ"]);contentCount=Convert.ToInt32(sdr["LianXiRenSLZDZ"]);}sdr.Close();}}}报错,必须声明标量变量"@ZhaoPianwhere"
解决方案
解决方案二:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Data.SqlClient;usingSystem.Configuration;usingSystem.IO;namespaceyonghudenglu{publicpartialclassfrmEdit:Form{publicfrmEdit(){InitializeComponent();}privatestringstrConn=ConfigurationManager.ConnectionStrings["yonghudenglu.Properties.Settings.TongXunGuanLiConnectionString"].ToString();publicstringtxtFilePath="";privateintgroupCount=0;privatestringstrUserName="";privateintcontentCount=0;publicbyte[]picData;privatevoidbtnCancel_Click(objectsender,EventArgse){DialogResult=DialogResult.Cancel;}privateintID;publicfrmEdit(intid){InitializeComponent();ID=id;}privatevoidpic_Click(objectsender,EventArgse){if(dlg.ShowDialog()==DialogResult.OK){pic.Image=Image.FromFile(dlg.FileName);txtFilePath=dlg.FileName;}}privatevoidbtnOK_Click(objectsender,EventArgse){stringname;if(txtName.Text==""){MessageBox.Show("联系人姓名不能为空!","添加失败",MessageBoxButtons.OK,MessageBoxIcon.Error);return;}else{name=txtName.Text;}stringgroup;if(cmbGroup.Text.Trim()==""){group="未分组";}else{group=cmbGroup.Text.Trim();}stringphone=txtPhone.Text.Trim();stringcompany=txtCompany.Text.Trim();stringemail=txtEmail.Text.Trim();byte[]b=null;if(txtFilePath!=""){try{FileStreamfs=newFileStream(txtFilePath,FileMode.Open,FileAccess.Read);intlen=Convert.ToInt32(fs.Length);b=newbyte[len];fs.Read(b,0,len);fs.Close();}catch{b=null;}}else{b=picData;}try{SqlConnectionconn=newSqlConnection(strConn);conn.Open();SqlCommandcmd_update=newSqlCommand();cmd_update.Connection=conn;cmd_update.CommandText="updateLianXiRensetZuBie=@ZuBie,XingMing=@XingMing,GongZuoDanWei=@GongZuoDanWei,LianXiDianHua=@LianXiDianHua,DianZiYouXiang=@DianZiYouXiang,ZhaoPian=@ZhaoPian";cmd_update.CommandText+="whereBianHao=@BianHao";cmd_update.Parameters.Add("@BianHao",SqlDbType.Int);cmd_update.Parameters.Add("@ZuBie",SqlDbType.VarChar,50);cmd_update.Parameters.Add("@XingMing",SqlDbType.VarChar,50);cmd_update.Parameters.Add("@GongZuoDanWei",SqlDbType.VarChar,500);cmd_update.Parameters.Add("@LianXiDianHua",SqlDbType.VarChar,200);cmd_update.Parameters.Add("@DianZiYouXiang",SqlDbType.VarChar,200);cmd_update.Parameters.Add("@ZhaoPian",SqlDbType.Image);cmd_update.Parameters["@BianHao"].Value=ID;cmd_update.Parameters["@ZuBie"].Value=group;cmd_update.Parameters["@XingMing"].Value=name;cmd_update.Parameters["@GongZuoDanWei"].Value=company;cmd_update.Parameters["@LianXiDianHua"].Value=phone;cmd_update.Parameters["@DianZiYouXiang"].Value=email;if(txtFilePath==""&&picData==null){cmd_update.Parameters["@ZhaoPian"].Value=DBNull.Value;}else{cmd_update.Parameters["@ZhaoPian"].Value=b;}cmd_update.ExecuteNonQuery();conn.Close();DialogResult=DialogResult.OK;}catch(Exceptionee){MessageBox.Show(ee.Message,"添加失败",MessageBoxButtons.OK,MessageBoxIcon.Error);return;}}protectedvoidLoadGroup(){try{cmbGroup.Items.Clear();SqlConnectionconn=newSqlConnection(strConn);SqlCommandcmd=newSqlCommand();cmd.Connection=conn;cmd.CommandText="selectdistinctZuBiefromLianXiRen";conn.Open();SqlDataReadersdr=cmd.ExecuteReader();while(sdr.Read()){cmbGroup.Items.Add(sdr[0].ToString());}sdr.Close();conn.Close();if(groupCount>0&&cmbGroup.Items.Count==groupCount){cmbGroup.DropDownStyle=ComboBoxStyle.DropDownList;}else{cmbGroup.DropDownStyle=ComboBoxStyle.DropDown;}}catch{}}privatevoidfrmEdit_Load(objectsender,EventArgse){strUserName=((frmMain)(this.Owner)).tsslUserName.Text;LoadGroup();SqlConnectionconn=newSqlConnection(strConn);SqlCommandcmd=newSqlCommand();cmd.Connection=conn;cmd.CommandText="select*fromLianXiRenwhereBianHao="+ID.ToString();SqlDataAdaptersda=newSqlDataAdapter(cmd);DataSetds=newDataSet();sda.Fill(ds);if(ds.Tables[0].Rows.Count==1){txtName.Text=ds.Tables[0].Rows[0]["XingMing"].ToString();cmbGroup.Text=ds.Tables[0].Rows[0]["ZuBie"].ToString();txtPhone.Text=ds.Tables[0].Rows[0]["LianXiDianHua"].ToString();txtCompany.Text=ds.Tables[0].Rows[0]["GongZuoDanWei"].ToString();txtEmail.Text=ds.Tables[0].Rows[0]["DianZiYouXiang"].ToString();if(ds.Tables[0].Rows[0]["ZhaoPian"]==DBNull.Value){pic.Image=yonghudenglu.Properties.Resources.png_0010;}else{byte[]b=(byte[])(ds.Tables[0].Rows[0]["ZhaoPian"]);pic.Image=Image.FromStream(newMemoryStream(b));picData=b;}}cmd.CommandText="select*fromYongHuwhereYongHuMing='"+strUserName+"'";conn.Open();SqlDataReadersdr=cmd.ExecuteReader();if(sdr.Read()){groupCount=Convert.ToInt32(sdr["ZuBieSLZDZ"]);contentCount=Convert.ToInt32(sdr["LianXiRenSLZDZ"]);}sdr.Close();}}}这是源代码!
解决方案三:
建议你的标题就能直接表达你的问题大概方向和意思
解决方案四:
这段代码里并没有用到@ZhaoPianwhere啊,是别的地方的问题吧
解决方案五:
数据库很冤枉的说:update语句。where之前请放一个空格。谢谢