导航:首页 > 数据分析 > 程序如何存进数据库

程序如何存进数据库

发布时间: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);

}

}

}

阅读全文

与程序如何存进数据库相关的资料

热点内容
ps3文件分割视频 浏览:280
微信图片一键转发软件 浏览:331
如何判断s200plc编程电缆 浏览:691
太原编程培训班哪个好 浏览:171
树叶吹奏教程 浏览:6
社交app带来了哪些社会问题 浏览:394
如何安装爱宝8800数据采集器 浏览:712
文件保存了怎么找不到了 浏览:476
彩票网站怎么辨真假 浏览:840
pr找不到该文件 浏览:963
java移除panel 浏览:354
jsp填充jsp 浏览:166
海关外贸大数据在哪里查 浏览:381
思特奇java笔试题 浏览:121
葫芦侠在手机中的文件名 浏览:813
plc编程应该怎么收钱 浏览:584
c语言中源文件由什么组成 浏览:890
linuxhttpdphp配置文件 浏览:607
拆单数据要怎么保存 浏览:17
mac电脑怎样压缩文件到100m 浏览:645

友情链接