SqlConnection conn=null;
public Form1()
{
InitializeComponent();
}
private void btnsave_Click(object sender, EventArgs e)
{
conn = new SqlConnection(connectionutil.getconnetionstr());
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from tb_image", conn);
SqlCommandBuilder mycb = new SqlCommandBuilder(da);
DataSet ds = new DataSet("MyImage");
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
FileStream fs = new FileStream("D://北京之旅1.jpg", FileMode.OpenOrCreate, FileAccess.Read);
byte[] MyData = new byte[fs.Length];
fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
将二进制文件写入sql数据库
最新推荐文章于 2024-07-06 16:26:34 发布
该博客介绍如何使用C#将二进制文件(如图片)读取为字节数据,并通过SqlConnection和DataSet保存到SQL数据库的特定表中。通过FileStream打开文件,读取内容到byte数组,然后创建新的数据行并更新到数据库。
摘要由CSDN通过智能技术生成