用C#如何将文件用二进制方式读出显示在文本框中?? 如何将一串二进制字符串保存为一个文件!!
1个回答
2013-08-28
展开全部
二进制方式读出显示在文本框中? 这个有点不太明白楼主的意思。只有图片可以直接显示。利用c#的文件流可以达到你想要的目的。下面的代码是将图片文件保存到数据库的,你可以看看FileStream fs = new FileStream(textBox2.Text.Trim(), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, bytes.Length);
string str = "server=DA5A491B3F7544B;database=study;uid=sa;pwd=liyi123";
SqlConnection con = new SqlConnection(str);
con.Open();
string sql = "insert into picture ([name],pic) values(@name,@pic)";
SqlCommand cmd = new SqlCommand(sql, con);
SqlParameter[] parm = new SqlParameter[]
{
new SqlParameter("@name",SqlDbType.VarChar,50),
new SqlParameter("@pic",SqlDbType.Image,bytes.Length)
};
parm[0].Value = textBox1.Text;
parm[1].Value = bytes;
cmd.Parameters.Add(parm[0]);
cmd.Parameters.Add(parm[1]);
if (cmd.ExecuteNonQuery() > 0)
MessageBox.Show("ok");
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, bytes.Length);
string str = "server=DA5A491B3F7544B;database=study;uid=sa;pwd=liyi123";
SqlConnection con = new SqlConnection(str);
con.Open();
string sql = "insert into picture ([name],pic) values(@name,@pic)";
SqlCommand cmd = new SqlCommand(sql, con);
SqlParameter[] parm = new SqlParameter[]
{
new SqlParameter("@name",SqlDbType.VarChar,50),
new SqlParameter("@pic",SqlDbType.Image,bytes.Length)
};
parm[0].Value = textBox1.Text;
parm[1].Value = bytes;
cmd.Parameters.Add(parm[0]);
cmd.Parameters.Add(parm[1]);
if (cmd.ExecuteNonQuery() > 0)
MessageBox.Show("ok");
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询