//filename为打开的文件名称 结果是把filename所示的文件读到MyData中
FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Read);
byte[] MyData = new byte[fs.Length];
fs.Read(MyData, 0, Convert.ToInt32(fs.Length));
fs.Close();
//textbox控件的TextChanged事件触发
private void textBox_bar_TextChanged(object sender, EventArgs e)//扫描打开
{
string fileNumber = textBox_bar.Text;//扫描枪得到的字符
string[] numbers = fileNumber.Split('\r');
fileNumber = numbers[0];
//fileNumber = fileNumber.Trim('\r');实现字符串的处理 剪去回车符
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename
=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");//连接数据库
con.Open();
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = "Select imgField From MyImages where fileNumber=@fileNumber";
cmd.Parameters.Add(new SqlParameter("@fileNumber", fileNumber));
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
byte[] MyData = null;
MyData = (byte[])reader["imgField"];
int ArraySize = new int();
ArraySize = MyData.GetUpperBound(0);
FileStream fs = new FileStream(@"C:\1T02Dwg1\1.dwg", FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(MyData, 0, ArraySize);
fs.Close(); //写数据库中得到的数据文件
string filepath = @"C:\1T02Dwg1\1.dwg";
this.axAvViewX1.src = filepath;
this.axAvViewX1.ZoomExtents();
this.Text = filepath;
}
else
{
MessageBox.Show("图纸不存在,请确认!");
}
}
con.Close();
}
}