C#读取文件并以二进制方式存入数据库

 

 

private void Save2DB(byte[] vbyte)
        {

            //待存入数据库的文件
            string AccPwd = "nsc";
            //待存入数据库的文件
            OleDbConnection myConnection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\RibbonGlobe\\bin\\Data\\modelData.mdb;Jet OLEDB:Database PassWord=" + AccPwd);
            myConnection.Open();


            string Sql_insert = "insert into ModelSourceList (Model_ID,Model_Name,Model_Attribute,Model_Data) values (@Model_ID ,@Model_Name, @Model_Attribute, @meshbuffer)";
            OleDbCommand myCommand = new OleDbCommand(Sql_insert, myConnection);
            try
            {
                //打开数据库连接
                //添加参数
                OleDbParameter[] param = new OleDbParameter[4];
                param[0] = new OleDbParameter("@Model_ID", OleDbType.VarChar);
                param[0].Value = '1';
                myCommand.Parameters.Add(param[0]);

                param[1] = new OleDbParameter("@Model_Name", OleDbType.VarChar);
                param[1].Value = '1';
                myCommand.Parameters.Add(param[1]);

                param[2] = new OleDbParameter("@Model_Attribute", OleDbType.VarChar);
                param[2].Value = '1';
                myCommand.Parameters.Add(param[2]);

                param[3] = new OleDbParameter("@byteColumn", OleDbType.Binary, vbyte.Length);
                param[3].Value = vbyte;
                myCommand.Parameters.Add(param[3]);
                //执行插入数据库
                int resultUp0 = myCommand.ExecuteNonQuery();
                if (resultUp0 == 1)
                    MessageBox.Show("插入完成!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                //     fs.Close();
                myConnection.Close();
            }

        }

 

 

你可能感兴趣的:(C#)