要實現在Sql Server中實現將檔案讀寫Word檔案,需要在要存取的表中新增Image型別的列,示例表結構為:
CREATE TABLE CONTRACTS (
ID VARCHAR (50),
CONTRACT_FILE IMAGE
);
要將Word檔案儲存到資料庫的CONTRACT_FILE欄位中,需要將檔案轉換為byte陣列,具體程式碼如下:
將檔案轉換為byte陣列
1 /// <summary>
2 /// 將檔案轉換為Bytes
3 /// </summary>
4 /// <param name="fileName"></param>
5 /// <returns></returns>
6 public static byte[] File2Bytes(string fileName)
7 {
8 FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Read);
9 byte[] fileDatas = new byte[fs.Length];
10 fs.Read(fileDatas, 0, System.Convert.ToInt32(fs.Length));
11 fs.Close();
12 return fileDatas;
13 }
然後將轉換完成的byte[]儲存到資料庫的對應欄位:
將檔案儲存到資料庫
2 /// 更新合同檔案
4 /// <param name="id"></param>
5 /// <param name="fileBytes"></param>
6 /// <returns></returns>
7 public bool UpdateContractFile(string id, byte[] fileBytes)
8 {
9 string sql = "UPDATE CONTRACTS SET CONTRACT_FILE=@CONTRACT_FILE WHERE ID=@ID";
10 using (SqlConnection conn = new SqlConnection(this.m_DataAccess.ConnectString))
11 {
12 conn.Open();
13 using (SqlCommand cmd = new SqlCommand())
14 {
15 cmd.Connection = conn;
16 cmd.CommandText = sql;
17 cmd.Parameters.Clear();
18
19 cmd.Parameters.Add(new SqlParameter("@CONTRACT_FILE", SqlDbType.Image));
20 cmd.Parameters["@CONTRACT_FILE"].Value = fileBytes;
21
22 cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.VarChar));
23 cmd.Parameters["@ID"].Value = id;
24
25 return cmd.ExecuteNonQuery() > 0 ? true : false;
26 }
27 }
28 }
要讀取資料庫中儲存的Word檔案,需要先將Image型別的欄位轉換為bytes[],具體程式碼如下:
透過ID獲取檔案byte陣列
2 /// 獲取合同檔案
6 public byte[] GetContractFile(string id)
8 string sql = "SELECT CONTRACT_FILE FROM CONTRACTS WHERE";
9 sql = string.Format(sql, id);
10 object contractFile;
11 contractFile = this.m_DataAccess.ExecuteScalar(sql);
12 if (contractFile == null)
13 {
14 return new byte[0];
15 }
16 else
17 {
18 return (byte[])contractFile;
19 }
20 }
在獲取到檔案的byte[]後,將該檔案透過檔案流操作儲存為Word檔案,具體程式碼如下:
將byte[]陣列儲存為Word檔案
1 byte[] fileBytes = this.m_ContractsBusiness.GetContractFile(id);
2 if (fileBytes.Length == 0)
3 {
4 XMessageBox.ShowError("未找到合同檔案!");
5 return;
6 }
7 SaveFileDialog sfd = new SaveFileDialog();
8 sfd.Filter = "Word檔案(*.doc)|*.doc";
9 if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
10 {
11 try
12 {
13 string saveFileName = sfd.FileName;
14 int arraysize = new int();//注意這句話
15 arraysize = fileBytes.GetUpperBound(0);
16 FileStream fs = new FileStream(saveFileName, FileMode.OpenOrCreate, FileAccess.Write);
17 fs.Write(fileBytes, 0, arraysize);
18 fs.Close();
19 if (XMessageBox.ShowQuestion("檔案下載成功,是否立即開啟檔案?") ==
20 System.Windows.Forms.DialogResult.Yes)
21 {
22 Process.Start(saveFileName);
23 }
24 }
25 catch (Exception ex)
26 {
27 XMessageBox.ShowError("下載檔案失敗!");
要實現在Sql Server中實現將檔案讀寫Word檔案,需要在要存取的表中新增Image型別的列,示例表結構為:
CREATE TABLE CONTRACTS (
ID VARCHAR (50),
CONTRACT_FILE IMAGE
);
要將Word檔案儲存到資料庫的CONTRACT_FILE欄位中,需要將檔案轉換為byte陣列,具體程式碼如下:
將檔案轉換為byte陣列
1 /// <summary>
2 /// 將檔案轉換為Bytes
3 /// </summary>
4 /// <param name="fileName"></param>
5 /// <returns></returns>
6 public static byte[] File2Bytes(string fileName)
7 {
8 FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Read);
9 byte[] fileDatas = new byte[fs.Length];
10 fs.Read(fileDatas, 0, System.Convert.ToInt32(fs.Length));
11 fs.Close();
12 return fileDatas;
13 }
然後將轉換完成的byte[]儲存到資料庫的對應欄位:
將檔案儲存到資料庫
1 /// <summary>
2 /// 更新合同檔案
3 /// </summary>
4 /// <param name="id"></param>
5 /// <param name="fileBytes"></param>
6 /// <returns></returns>
7 public bool UpdateContractFile(string id, byte[] fileBytes)
8 {
9 string sql = "UPDATE CONTRACTS SET CONTRACT_FILE=@CONTRACT_FILE WHERE ID=@ID";
10 using (SqlConnection conn = new SqlConnection(this.m_DataAccess.ConnectString))
11 {
12 conn.Open();
13 using (SqlCommand cmd = new SqlCommand())
14 {
15 cmd.Connection = conn;
16 cmd.CommandText = sql;
17 cmd.Parameters.Clear();
18
19 cmd.Parameters.Add(new SqlParameter("@CONTRACT_FILE", SqlDbType.Image));
20 cmd.Parameters["@CONTRACT_FILE"].Value = fileBytes;
21
22 cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.VarChar));
23 cmd.Parameters["@ID"].Value = id;
24
25 return cmd.ExecuteNonQuery() > 0 ? true : false;
26 }
27 }
28 }
然後將轉換完成的byte[]儲存到資料庫的對應欄位:
將檔案儲存到資料庫
1 /// <summary>
2 /// 更新合同檔案
3 /// </summary>
4 /// <param name="id"></param>
5 /// <param name="fileBytes"></param>
6 /// <returns></returns>
7 public bool UpdateContractFile(string id, byte[] fileBytes)
8 {
9 string sql = "UPDATE CONTRACTS SET CONTRACT_FILE=@CONTRACT_FILE WHERE ID=@ID";
10 using (SqlConnection conn = new SqlConnection(this.m_DataAccess.ConnectString))
11 {
12 conn.Open();
13 using (SqlCommand cmd = new SqlCommand())
14 {
15 cmd.Connection = conn;
16 cmd.CommandText = sql;
17 cmd.Parameters.Clear();
18
19 cmd.Parameters.Add(new SqlParameter("@CONTRACT_FILE", SqlDbType.Image));
20 cmd.Parameters["@CONTRACT_FILE"].Value = fileBytes;
21
22 cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.VarChar));
23 cmd.Parameters["@ID"].Value = id;
24
25 return cmd.ExecuteNonQuery() > 0 ? true : false;
26 }
27 }
28 }
要讀取資料庫中儲存的Word檔案,需要先將Image型別的欄位轉換為bytes[],具體程式碼如下:
透過ID獲取檔案byte陣列
1 /// <summary>
2 /// 獲取合同檔案
3 /// </summary>
4 /// <param name="id"></param>
5 /// <returns></returns>
6 public byte[] GetContractFile(string id)
7 {
8 string sql = "SELECT CONTRACT_FILE FROM CONTRACTS WHERE";
9 sql = string.Format(sql, id);
10 object contractFile;
11 contractFile = this.m_DataAccess.ExecuteScalar(sql);
12 if (contractFile == null)
13 {
14 return new byte[0];
15 }
16 else
17 {
18 return (byte[])contractFile;
19 }
20 }
在獲取到檔案的byte[]後,將該檔案透過檔案流操作儲存為Word檔案,具體程式碼如下:
將byte[]陣列儲存為Word檔案
1 byte[] fileBytes = this.m_ContractsBusiness.GetContractFile(id);
2 if (fileBytes.Length == 0)
3 {
4 XMessageBox.ShowError("未找到合同檔案!");
5 return;
6 }
7 SaveFileDialog sfd = new SaveFileDialog();
8 sfd.Filter = "Word檔案(*.doc)|*.doc";
9 if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
10 {
11 try
12 {
13 string saveFileName = sfd.FileName;
14 int arraysize = new int();//注意這句話
15 arraysize = fileBytes.GetUpperBound(0);
16 FileStream fs = new FileStream(saveFileName, FileMode.OpenOrCreate, FileAccess.Write);
17 fs.Write(fileBytes, 0, arraysize);
18 fs.Close();
19 if (XMessageBox.ShowQuestion("檔案下載成功,是否立即開啟檔案?") ==
20 System.Windows.Forms.DialogResult.Yes)
21 {
22 Process.Start(saveFileName);
23 }
24 }
25 catch (Exception ex)
26 {
27 XMessageBox.ShowError("下載檔案失敗!");
28 }