private void pictureBox1_DoubleClick(object sender, System.EventArgs e)
{
try
{
this.openFileDialog1.Filter = "BMP 图片 (*.bmp)|*.bmp|JPG 图片 (*.jpg)|*.jpg|GIF 图片 (*.gif)|*.gif|JPEG 图片(*.jpeg)|*.jpeg\" ";
openFileDialog1.Title = "请选择图片";
if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
{
this.pictureBox1.Image=Image.FromFile(this.openFileDialog1.FileName);
}
FileStream fs=new FileStream(this.openFileDialog1.FileName,FileMode.OpenOrCreate,FileAccess.Read);
theData=new byte[fs.Length];
fs.Read(theData,0,System.Convert.ToInt32(fs.Length));
fs.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
////上面为读取图片,下面保存
sqlcon.Open();
DataSet ds=new DataSet();
SqlDataAdapter da = new SqlDataAdapter("Select top 10 * From Product",sqlcon);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
/* da.SelectCommand.CommandText="select * from Product";
da.SelectCommand.Connection=sqlcon;
da.InsertCommand.Connection=sqlcon;
SqlCommand sqlcom=new SqlCommand("Insert into Product(Productdh,Productlb,sccs,Productjj,Particular,Pic) values (@Productdh,@Productlb,@sccs,@Productjj,@Particular,@Pic)",sqlcon);
//sqlcom.CommandType=CommandType.StoredProcedure;
sqlcom.Parameters.Add("@Productdh",SqlDbType.VarChar,13);
sqlcom.Parameters["@Productdh"].Value=productdh;
sqlcom.Parameters.Add("@Productlb",SqlDbType.VarChar,10);
sqlcom.Parameters["@Productlb"].Value=e1;
sqlcom.Parameters.Add("@sccs",SqlDbType.VarChar,30);
sqlcom.Parameters["@sccs"].Value=textBox3.Text;
sqlcom.Parameters.Add("@Productjj",SqlDbType.VarChar,100);
sqlcom.Parameters["@Productjj"].Value=textBox4.Text;
sqlcom.Parameters.Add("@Particular",SqlDbType.Text,16);
sqlcom.Parameters["@Particular"].Value=richTextBox1.Text;
sqlcom.Parameters.Add("@Pic",SqlDbType.Image,16);
sqlcom.Parameters["@Pic"].Value=theData;
//sqlcom.Prepare();
da.InsertCommand=sqlcom;
*/
da.Fill(ds,"MyImages");
DataRow myRow;
myRow=ds.Tables["MyImages"].NewRow();
myRow["Productdh"] = productdh;
myRow["Productlb"] = e1;
myRow["sccs"] = textBox3.Text;
myRow["Productjj"] =textBox4.Text;
myRow["Particular"] =richTextBox2.Text;
myRow["Addtime"]=DateTime.Now.ToLongTimeString();
myRow["Productlbming"]=textBox1.Text;
try
{
if(theData.Length>0)
{
myRow["Pic"] =theData;
}
else
{
myRow["Pic"]=null;
}
}
catch{}
try
{
ds.Tables["MyImages"].Rows.Add(myRow);
da.Update(ds, "MyImages");
//da.InsertCommand.ExecuteNonQuery();
MessageBox.Show("录入产品成功");
{
try
{
this.openFileDialog1.Filter = "BMP 图片 (*.bmp)|*.bmp|JPG 图片 (*.jpg)|*.jpg|GIF 图片 (*.gif)|*.gif|JPEG 图片(*.jpeg)|*.jpeg\" ";
openFileDialog1.Title = "请选择图片";
if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
{
this.pictureBox1.Image=Image.FromFile(this.openFileDialog1.FileName);
}
FileStream fs=new FileStream(this.openFileDialog1.FileName,FileMode.OpenOrCreate,FileAccess.Read);
theData=new byte[fs.Length];
fs.Read(theData,0,System.Convert.ToInt32(fs.Length));
fs.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
////上面为读取图片,下面保存
sqlcon.Open();
DataSet ds=new DataSet();
SqlDataAdapter da = new SqlDataAdapter("Select top 10 * From Product",sqlcon);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
/* da.SelectCommand.CommandText="select * from Product";
da.SelectCommand.Connection=sqlcon;
da.InsertCommand.Connection=sqlcon;
SqlCommand sqlcom=new SqlCommand("Insert into Product(Productdh,Productlb,sccs,Productjj,Particular,Pic) values (@Productdh,@Productlb,@sccs,@Productjj,@Particular,@Pic)",sqlcon);
//sqlcom.CommandType=CommandType.StoredProcedure;
sqlcom.Parameters.Add("@Productdh",SqlDbType.VarChar,13);
sqlcom.Parameters["@Productdh"].Value=productdh;
sqlcom.Parameters.Add("@Productlb",SqlDbType.VarChar,10);
sqlcom.Parameters["@Productlb"].Value=e1;
sqlcom.Parameters.Add("@sccs",SqlDbType.VarChar,30);
sqlcom.Parameters["@sccs"].Value=textBox3.Text;
sqlcom.Parameters.Add("@Productjj",SqlDbType.VarChar,100);
sqlcom.Parameters["@Productjj"].Value=textBox4.Text;
sqlcom.Parameters.Add("@Particular",SqlDbType.Text,16);
sqlcom.Parameters["@Particular"].Value=richTextBox1.Text;
sqlcom.Parameters.Add("@Pic",SqlDbType.Image,16);
sqlcom.Parameters["@Pic"].Value=theData;
//sqlcom.Prepare();
da.InsertCommand=sqlcom;
*/
da.Fill(ds,"MyImages");
DataRow myRow;
myRow=ds.Tables["MyImages"].NewRow();
myRow["Productdh"] = productdh;
myRow["Productlb"] = e1;
myRow["sccs"] = textBox3.Text;
myRow["Productjj"] =textBox4.Text;
myRow["Particular"] =richTextBox2.Text;
myRow["Addtime"]=DateTime.Now.ToLongTimeString();
myRow["Productlbming"]=textBox1.Text;
try
{
if(theData.Length>0)
{
myRow["Pic"] =theData;
}
else
{
myRow["Pic"]=null;
}
}
catch{}
try
{
ds.Tables["MyImages"].Rows.Add(myRow);
da.Update(ds, "MyImages");
//da.InsertCommand.ExecuteNonQuery();
MessageBox.Show("录入产品成功");
解决方案 »
- panel 和Groupbox
- 黑简单的问题,c#如何操作HTML?
- 在C#中怎么打印Panel里的控件??
- 索引器在开发过程中,用在什么地方??
- vs2005 c#利用DirectXSDK做录音程序,要同时录多个音频源,为什么在分配了14快缓冲区之后不能在获得新的缓冲区了????
- GDI画图从右到左显示怎么实现
- 精通asp,asp.net三年工作经验,现在准备考研,但是需要找份兼职糊口,每月2000左右就成,各位给点意见
- 有关RepositoryItemLookUpEdit的查询(DevExpress的问题)
- 关于解析Expression
- 定义定长字符的结构
- 请问C#里有没有关于声音的类,就象关于图像的Image类一样?
- 如何使用C#将图象文件存入SQL Server ?
哪来的数据库连接呢?
//将图片保存到数据库中
if(this.picPhoto.Image==null)
{
m_DataRow[MyTools.g_PhotoField]=DBNull.Value;
}
else
{
try
{
MemoryStream ms = new MemoryStream ();
picPhoto.Image.Save (ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte [] myData = new Byte [ms.Length ];
ms.Position = 0;
ms.Read (myData,0,Convert.ToInt32 (ms.Length ));
m_DataRow[MyTools.g_PhotoField] = myData; }
catch(System.Exception ee)
{
MessageBox.Show(ee.Message);
}
}//else//读取图象
if(this.m_DataRow[MyTools.g_PhotoField]!=DBNull.Value)
{
try
{
Byte[] byteBLOBData = new Byte[0];
byteBLOBData = (Byte[])m_DataRow[MyTools.g_PhotoField];
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
this.picPhoto.Image= Image.FromStream(stmBLOBData);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
this.picPhoto.Image= null;
}