
Sqlmand是 *** 作sql数据库的,Access用OleDbmand首先定义一个链接对象OleDbConnection conn = new OleDbConnection("[数据库连接字符串]");conn.Open(); 打开数据库连接OleDbmand cmd = new OleDbmand("[Insert/Update/Delte语句]", conn)cmd.ExecuteNonQuery(); 执行 *** 作,如果是查询则不是用这个方法 最后别忘记关闭数据库连接和释放对象。
2. C# 如何将 图片直接存入SQL数据库中
把文件转成二进制流出入数据库
private void button2_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(textBox1.Text, FileMode.Open)
BinaryReader br = new BinaryReader(fs)
Byte[] byData = br.ReadBytes((int)fs.Length)
fs.Close()
string conn = "server=.database=testDBUid=saPwd=sa "
SqlConnection myconn = new SqlConnection(conn)
myconn.Open()
string str = "insert into pro_table (pro_name,pro_file) values('测试文件',@file)"
Sqlmand mym = new Sqlmand(str, myconn)
mym.Parameters.Add("@file", SqlDbType.Binary, byData.Length)
mym.Parameters["@file"].Value = byData
mym.ExecuteNonQuery()
myconn.Close()
}
从数据库中把二进制流读出写入还原成文件
private void button4_Click(object sender, EventArgs e)
{
string conn = "server=.database=testDBUid=saPwd=sa "
string str = "select pro_file from pro_table where pro_name='测试文件' ";
SqlConnection myconn = new SqlConnection(conn)
SqlDataAdapter sda = new SqlDataAdapter(str, conn)
DataSet myds = new DataSet()
myconn.Open()
sda.Fill(myds)
myconn.Close()
Byte[] Files = (Byte[])myds.Tables[0].Rows[0]["pro_file"]
BinaryWriter bw = new BinaryWriter(File.Open("D:\\2.rdlc",FileMode.OpenOrCreate))
bw.Write(Files)
bw.Close()
}
3. c#如何把图片存取到SQL数据库
一楼开玩笑了!!可以保存到数据库的。
首先,你的数据库里要有一个存放二进制数据的字段。然后,用一个文件选择控件,让用户选择图片。
用FileStream.Read把图片文件按照二进制读取到byte[]中,接下来,链接数据库,用sql语句,进行相应的插入 *** 作,将数据库的二进制数据的字段赋值为byte[]就行了。以下是保存和显示的代码:private void SaveImage(string fileName) { Read the file into a byte array using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { byte[] imageData = new Byte[fs.Length]fs.Read(imageData, 0, (int)fs.Length)using (SqlConnection conn = new SqlConnection(connectionString)) { string sql = "insert into image (imagefilename,blobdata) values (@filename,@blobdata)"Sqlmand cmd = new Sqlmand(sql, conn)cmd.Parameters.Add("@filename",SqlDbType.Text)cmd.Parameters["@filename"].Direction = ParameterDirection.Inputcmd.Parameters.Add("@blobdata", SqlDbType.Image)cmd.Parameters["@blobdata"].Direction = ParameterDirection.InputStore the byte array within the image field cmd.Parameters["@filename"].Value = fileNamecmd.Parameters["@blobdata"].Value = imageDataconn.Open()if (cmd.ExecuteNonQuery() == 1) { MessageBox.Show("Done")} } } } private void LoadImage(string fileName) { using (SqlConnection conn = new SqlConnection(connectionString)) { string sql = "select blobdata from Image where ImageFileName like @filename"Sqlmand cmd = new Sqlmand(sql, conn)cmd.Parameters.Add("@filename", SqlDbType.Text)cmd.Parameters["@filename"].Value = fileNameconn.Open()object objImage = cmd.ExecuteScalar()byte[] buffer = (byte[])objImageBinaryWriter bw = new BinaryWriter(new FileStream("C:\\abcd.", FileMode.Create))bw.Write(buffer)bw.Close()MemoryStream ms = new MemoryStream(buffer)Image bgImage = Image.FromStream(ms)ms.Close()this.BackgroundImage = bgImage} } ------------------------------------------------------------------PS:用空情报我踩踩空间,谢谢。
/kxl361。
4. 如何将文件的路径存入SQL数据库中去
建一个表:JpgFiles,其中至少包含一个列:JpgPath,用来存放绝对路径的字符串,所以这个列需要用varchar,长度假设为50,不够的话自己再增加。
string strPath = @"d:\baidu\up"
string[] fileNames = System.IO.Directory.GetFiles(strPath)
SqlConnection Cn=new SqlConnection(这里写你的连接串);
Sqlmand Cmd=new Sqlmand("Insert JpgFiles values (@JpgPath)",Cn)
Cmd.Parameters.Add("@JpgPath",SqlDbType.VarChar,50)
foreach (string strName in fileNames)
{
Cmd.Parameters[0].Value=strName
Cmd.ExecuteNoQuery()
}
5. 怎样将数据存入mysql数据库
MySQL命令行导出数据库:
1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录
如我输入的命令行:cd C:\Program Files\MySQL\MySQL Server 4.1\bin
(或者直接将windows的环境变量path中添加该目录)
2,导出数据库:mysqldump -u 用户名 -p 数据库名 >; 导出的文件名
如我输入的命令行:mysqldump -u root -p news >news.sql (输入后会让你输入进入MySQL的密码)
(如果导出单张表的话在数据库名后面输入表名即可)
3、会看到文件news.sql自动生成到bin文件下
命令行导入数据库:
1,将要导入的.sql文件移至bin文件下,这样的路径比较方便
2,同上面导出的第1步
3,进入MySQL:mysql -u 用户名 -p
如我输入的命令行:mysql -u root -p (输入同样后会让你输入MySQL的密码)
4,在MySQL-Front中新建你要建的数据库,这时是空数据库,如新建一个名为news的目标数据库
5,输入:mysql>use 目标数据库名
如我输入的命令行:mysql>use news
6,导入文件:mysql>source 导入的文件名;
如我输入的命令行:mysql>source news.sql
6. 如何把文件存入到sql server 2008
1.MDF文件
在企业管理器中
右击数据库
点击所有任务
附加数据库
点三个点选择文件
选中U盘中的MDF文件确定即可
2.BAK等备份文件:
新建空数据库,取名最好为原数据库名.
右击新建的数据库
点所有任务
点还原数据库
点从设备
点选择设备
点添加
定位到U盘中您的备份的文件
确定
点选项
点在现有数据库上强制还原
点确定
等待
完成!
另外,站长团上有产品团购,便宜有保证
首先设置Form的AllowDrop=true
public partial class Form1 : Form{
public Form1()
{
InitializeComponent()
}
private void btnStart_Click(object sender, EventArgs e)
{
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Link
}
else
{
e.Effect = DragDropEffects.None
}
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString()
// 接下来可以通过filestream来上传文件。
}
}
在DragDrop事件中能够得到拖放到窗体上的文件路径,然后使用filestream就可以上传了。
首先,将你要保存音频文件的数据库表的列的数据类型设置为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 = liuxueqinInitial Catalog=MyTestIntegrated 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().MainModule.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=liuxueqinInitial Catalog=MyTestIntegrated 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().MainModule.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)
}
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)