我找到一些代码,但不好用,不懂C#,所以来这里请教大家了! SqlConnection conn =null; SqlCommand cmd = null; SqlParameter param = null; FileStream fs = null; const string sConn = "server=(local);Initial Catalog=Northwind;UID=ctester;PWD=password"; try { conn = new SqlConnection(sConn); cmd = new SqlCommand("UPDATE Categories SET Picture = ?Picture WHERE CategoryName = 'Seafood'", conn); fs = new FileStream("c:\\Builder.doc", FileMode.Open, FileAccess.Read); Byte[] blob = new Byte[fs.Length]; fs.Read(blob, 0, blob.Length); fs.Close(); param = new SqlParameter("Picture", SqlDbType.VarBinary, blob.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, blob); cmd.Parameters.Add(param); conn.Open(); cmd.ExecuteNonQuery(); } catch (SqlException e){ Console.Write("SQL Exception: " + e.Message()); } catch (Exception e) { Console.Write("Exception: " e.Message()); }实际上,就是如何将一个 byte[] 更新到mysql的BLOB上面,不是Insert 是 update 操作!
解决方案 »
- 数据类型转换
- 一个页面中有一个DataGrid和一个DataList,现在翻页功能有问题,求助
- 十万火急,求助
- winform中的datagrid中有两个绑定字典表的comboBox列,例如学院和系,如何做到修改学院列后,绑定的系别列也跟随变化
- 更换imagelist后产生的问题(急50分求解)
- 如何用c#操作binary字段?
- 在.net下怎么封IP
- 我建了两个项目,现在一个项目中要引用另一个,怎么引用?
- WinForm程序怎么修改Internet选项安全属性?
- 我有一个string的“123”字串,怎么样转换为int,赋值给整形变量?
- C#有没有办法获取DLL里面的函数名?DllImport出了点问题
- c# 窗体 选择自定义方法
cmd = new SqlCommand("UPDATE Categories SET Picture =@Picture Picture WHERE
new SqlParameter("Picture", byte[])
如果文件比较大的话,需要循环读取文件内容,。
MSSQL下面是:
UPDATETEXT { table_name.dest_column_name dest_text_ptr }
{ NULL | insert_offset }
{ NULL | delete_length }
[ WITH LOG ]
[ inserted_data
| { table_name.src_column_name src_text_ptr } ]如果2005等较新的版本,用write 子 句的update.
USE AdventureWorks;
GO
DECLARE @MyTableVar table (
DocumentID int NOT NULL,
SummaryBefore nvarchar(max),
SummaryAfter nvarchar(max));
UPDATE Production.Document
SET DocumentSummary .WRITE (N'features',28,10)
OUTPUT INSERTED.DocumentID,
DELETED.DocumentSummary,
INSERTED.DocumentSummary
INTO @MyTableVar
WHERE DocumentID = 3 ;
SELECT DocumentID, SummaryBefore, SummaryAfter
FROM @MyTableVar;
GO
OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings["NEWOracleConn"].ToString());
OracleCommand cmd = new OracleCommand("UPDATE TUSER SET PHOTO=:photo WHERE userid=:id", conn);
Console.WriteLine(cmd.CommandText);
cmd.Parameters.Add("photo",OracleType.Blob);
cmd.Parameters["photo"].Value = photo;
cmd.Parameters.Add("id", OracleType.Number);
cmd.Parameters["id"].Value = id;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close(); 参考 我这段代码是更新Oracle中的blob字段
cmm = new MySql.Data.MySqlClient.MySqlCommand("", connect);
MySql.Data.MySqlClient.MySqlParameter param = null;
param = new MySql.Data.MySqlClient.MySqlParameter("?Picture", System.Data.DbType.Binary);
param.Value = System.Text.Encoding.Default.GetBytes("");
cmm.Parameters.Add(param);
cmm.ExecuteNonQuery();
OracleCommand cmd = new OracleCommand("UPDATE TUSER SET PHOTO=:photo WHERE userid=:id", conn);
//注意这里是 冒号 不是@ 也不是? 我是使用的OracleClien操作的Oracle
http://bbs.erp100.com/?fromuid=155581
这个和 System.Data.SqlClient 什么都一样.
估计 System.Data.MySqlClient 已经自动帮我们把byte[] 转换了
不过没试过.楼主好运
.
http://dev.mysql.com/downloads/connector/net/6.1.html
直接用OdbcConnection 用其它上层数据库连接类的也有,如果需要可以给你。int UpdateBlob(OdbcConnection^ m_dbConn, String^ FileName,int docID)
{
//you should put following code into try catch
//support you have a table with 2 columns: docID and imgfile//read image file
FileStream^ fs = gcnew FileStream(FileName, FileMode::Open, FileAccess::Read);
int FileSize = (int)fs->Length;
array<Byte>^ rawData = gcnew array<Byte>(FileSize);
fs->Read(rawData, 0, FileSize);
fs->Close();
//make sql
String^ sql = String::Format("UPDATE table1 VALUES({0},?)",docID);
OdbcCommand^ cmd = gcnew OdbcCommand(sql,m_dbConn);
cmd->Parameters->Add("imgfile",OdbcType::Binary,FileSize,"imgfile")->Value = rawData;//do update now
ret = cmd->ExecuteNonQuery();
return ret;
}
我以前在c#中连接过MySQL,上面的链接有介绍;像 string sql = "update table_name set col_name='" + values + "'where col2='" + values2 + "'";完成后就执行 RunSqlDatacmd(sql)就好了,RunSqlDatacmd(sql)代码如下: public static long RunSqlDatacmd(string sql) //sql语句执行成员
{
MySqlConnection dbconn = new MySqlConnection("Database='Online';Data Source='localhost';User Id='root';Password='123456';charset=utf8");
MySqlCommand cmd = dbconn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
long ret = 0;
try
{
if (cmd.Connection.State == ConnectionState.Broken)
{
cmd.Connection.Close();
cmd.Connection.Open();
}
else if (cmd.Connection.State == ConnectionState.Closed)
{
cmd.Connection.Open();
}
// else if (cmd.Connection.State == ConnectionState.Open)
// {
ret = cmd.ExecuteNonQuery();
// }
//else
// {
// ret = -102;
// }
}
catch (Exception ex)
{
string m = ex.Message;
ret = -5;
}
cmd.Dispose();
dbconn.Close();
return ret;
} }
}
string title ,string wjtype , int size, byte[] buffByte)
{
string comm = "insert s_pic(Pid,sysname,username,IP,title,wjtype,size,pic,others,queryers,deleters)" +
" values({0},'{1}','{2}','{3}','{4}','{5}',{6},@img,'','','')";
// @"Insert into table1(img,name) values(@img,@name)";
SqlCommand sqlCommand1 = new System.Data.SqlClient.SqlCommand();
sqlCommand1.CommandType = System.Data.CommandType.Text;
sqlCommand1.CommandText = string.Format( comm ,Pid ,sysname ,username ,Ip ,title ,wjtype ,size ) ;
sqlCommand1.Connection = Connection;
//创建Parameter
sqlCommand1.Parameters.Add("@img", System.Data.SqlDbType.Image);
sqlCommand1.Parameters[0].Value = buffByte;
try
{
Connection.Open();
sqlCommand1.ExecuteNonQuery();
m_bln = true;
}
catch { m_bln = false; }
finally
{
Connection.Close();
Connection.Dispose();
}
buffByte = null;
}
JAVA入门很容易,深入很难
.net 入门很容易,深入也比较容易精通的都是思想,不是工具。
还是顶哈。。
int yA_ = this.lblLTA.Location.Y;
int hA_ = this.lblLTA.Height;
this.lblLTA.Tag = this.lblLTA_.Tag.ToString();
int A_ = Convert.ToInt32(this.lblLTA_.Tag) * 3;
this.lblLTA.Location = new Point(xA_, yA_ - A_);
this.lblLTA.Height = hA_ + A_;
this.lblLTA.Location = new Point(xA_, yA_ - A_);
this.lblLTA.Height = hA_ + A_;
http://www.xinkeor.cn/?11604-1.html
http://www.xinkeor.cn/?11604-1.html