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

程序如何存進資料庫

發布時間: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);

}

}

}

閱讀全文

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

熱點內容
怎麼知道網路商 瀏覽:629
卸載驅動顯示找不到文件 瀏覽:66
編程抄碼怎麼找 瀏覽:29
pm編程用戶定義怎麼轉換邊界呢 瀏覽:262
如何建立在線編程課堂 瀏覽:386
開關燈一行四個代碼在線製作 瀏覽:217
zkt打卡機的數據怎麼導出 瀏覽:559
用手機微信發文件 瀏覽:909
蘋果系統壓縮文件 瀏覽:62
linux版本有什麼不同 瀏覽:389
念皇90版本武器選擇 瀏覽:60
雲盤里的文件存在哪裡 瀏覽:170
blog資料庫表結構 瀏覽:462
去文件夾只讀屬性 瀏覽:604
jspurl跳轉 瀏覽:422
lol怎樣用id查qq 瀏覽:696
javastreamclose 瀏覽:61
macu盤隱藏文件夾 瀏覽:396
電腦讀取文件的格式 瀏覽:738
百度手機瀏覽器50版本下載目錄 瀏覽:969

友情鏈接