導航:首頁 > 數據分析 > 程序如何存進資料庫

程序如何存進資料庫

發布時間:2024-07-31 01:59:00

① 如何把excel表格數據導入到資料庫

1、打開SQL Server 2014 Management Studio 資料庫,並且登錄進去;

② C#或者VB,如何把各種文件保存進資料庫

首先,將你要保存音頻文件的資料庫表的列的數據類型設置為image(for sqlserver);
然後,將要保存的文件以流的形式讀取到一個byte[]中;
最後使用標準的insert語句就可以了。

下面附上示例代碼
創建項目

1. 添加一個名為RWTest的表到 SQL Server MYTest 資料庫。 表欄位設置如下:

a. 唯一標識欄位名稱為"ID",類型為Int。

b. 名稱為"Description"的VarChar類型的欄位,欄位長度為50。

c. 名稱為"ImgField" 的Image 類型的欄位。

2. 啟動 Visual Studio .NET, 並創建一個新的 Visual C# Windows 應用程序項目。

3. 從工具欄中拖兩個Button 控制項到默認窗體, Form1。

4. 在屬性窗口中修改Name為buttonFileToDB, Text 屬性為從文件保存到資料庫, 然後修改Name為buttonDBToFile ,Text 屬性為從資料庫保存到文件。

5 從工具欄放置2個TextBox和1個PictureBox控制項:Name屬性分別為:textBoxGetID, textBoxGetDescription, pictureBoxGetImage, 顯示從資料庫讀出的ID,Description,ImgField欄位。

源碼實例

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

using System.IO;

using System.Collections;

//資料庫說明:MyTest資料庫,RWTest表,包含3列:ID(int),Description(varchar(50),ImgField(Image)

namespace RWImgSQL

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void buttonFileToDB_Click(object sender, EventArgs e)

{

SqlConnection sqlConnection = new SqlConnection("Data Source = liuxueqin; Initial Catalog=MyTest;Integrated Security=True");

SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("Select * from RWTest", sqlConnection);

SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);

DataSet dataSet = new DataSet("RWTest");

sqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;//確定現有 DataSet 架構與傳入數據不匹配時需要執行的操作。

String CurrentExeName = System.Diagnostics.Process.GetCurrentProcess().MainMole.FileName;

string ImageFile = System.IO.Path.GetDirectoryName(CurrentExeName) + "\\F1.jpg";

System.IO.FileStream fileStream = new FileStream(ImageFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);

byte[] myData = new byte[fileStream.Length];

fileStream.Read(myData, 0, System.Convert.ToInt32(fileStream.Length));//從流中讀取位元組塊,並將數據寫入到該緩沖區

fileStream.Close();

try

{

sqlDataAdapter.Fill(dataSet, "RWTest");

//DataRow表示DataTable中的一行數據

System.Data.DataRow dataRow;

dataRow = dataSet.Tables["RWTest"].NewRow();

dataRow1["ID"] = 1;

dataRow1["Description"] = "This would be description text";

dataRow1["ImgField"] = myData;

dataSet.Tables["RWTest"].Rows.Add(dataRow);

sqlDataAdapter.Update(dataSet, "RWTest");

sqlConnection.Close();

MessageBox.Show("寫入資料庫成功!", " 信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

catch (Exception ex)

{

if (sqlConnection.State == ConnectionState.Open)

{

sqlConnection.Close();

}

MessageBox.Show("寫入資料庫失敗"+ex.Message, " 信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

}

private void buttonDBToFile_Click(object sender, EventArgs e)

{

SqlConnection sqlConnection = new SqlConnection("Data Source=liuxueqin;Initial Catalog=MyTest;Integrated Security=True");

SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("Select * from RWTest", sqlConnection);

SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);

DataSet dataSet = new DataSet("RWTest");

byte[] MyData = new byte[0];

sqlDataAdapter.Fill(dataSet, "RWTest");

DataRow myRow;

myRow = dataSet.Tables["RWTest"].Rows[0];

MyData = (byte[])myRow["imgField"];

int ArraySize = MyData.GetUpperBound(0);

String CurrentExeName = System.Diagnostics.Process.GetCurrentProcess().MainMole.FileName;

string ImageFile = System.IO.Path.GetDirectoryName(CurrentExeName) + "\\F2.jpg";

FileStream fs = new FileStream(ImageFile, FileMode.OpenOrCreate, FileAccess.Write);

fs.Write(MyData, 0, ArraySize);

fs.Close();

//---在界面上的2個textBox和1個pictureBox,用來顯示從資料庫中讀出的ID,Description,ImageField欄位

textBoxGetID.Text = myRow["ID"].ToString();

textBoxGetDescription.Text = myRow["Description"].ToString();

pictureBoxGetImage.Image = Image.FromFile(ImageFile);

if (sqlConnection.State == ConnectionState.Open)

{

sqlConnection.Close();

}

MessageBox.Show(" 從資料庫讀出數據成功!", " 信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

}

}

閱讀全文

與程序如何存進資料庫相關的資料

熱點內容
工商銀行app人工服務在哪裡 瀏覽:19
怎麼讓文件中全體內容往下移 瀏覽:783
魔獸爭霸126版本轉換器 瀏覽:984
word2003精簡版下載 瀏覽:703
微博跳轉appstore 瀏覽:537
打開文件時許可權a代表什麼 瀏覽:155
昆侖通態導出數據文件名 瀏覽:338
手機遷移數據為什麼需要重新登錄 瀏覽:958
錄入資料庫的圖片如何更改 瀏覽:132
怎樣獲取郵箱帳號和密碼 瀏覽:809
怎麼通過js實現回到指定頁面 瀏覽:140
如何用網路簽字 瀏覽:552
三星電視拆機教程 瀏覽:19
創維怎麼連接網路 瀏覽:868
2007版word繪圖在哪裡 瀏覽:311
可以拍車牌的app是什麼 瀏覽:508
文件加個井字型大小什麼意思 瀏覽:155
怎麼刪除多重網路 瀏覽:999
求生之路2區域網聯機工具 瀏覽:827
說明文件結尾用什麼詞 瀏覽:578

友情鏈接