Mysql上传文件. [英] Mysql Upload File.
本文介绍了Mysql上传文件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嘿.
推荐答案
public class DataAccess
{
private string _strConn =
@"Driver= {MySQLODBC 3.51 Driver};SERVER=localhost;DATABASE=test;";
private OdbcConnection _objConn;
public DataAccess()
{
this._objConn = new OdbcConnection(this._strConn);
}
// This function adds the Images to database
public string addImage(byte [] buffer,string extension)
{
string strSql = "SELECT * FROM File";
DataSet ds = new DataSet("Image");
OdbcDataAdapter tempAP = new OdbcDataAdapter(strSql,this._objConn);
OdbcCommandBuilder objCommand = new OdbcCommandBuilder(tempAP);
tempAP.Fill(ds,"Table");
try
{
this._objConn.Open();
DataRow objNewRow = ds.Tables["Table"].NewRow();
objNewRow["Extension"] = extension;
objNewRow["Data"] = buffer;
ds.Tables["Table"].Rows.Add(objNewRow);
// trying to update the table to add the image
tempAP.Update(ds,"Table");
}
catch(Exception e){return e.Message;}
finally{this._objConn.Close();}
return null;
}
// This function to get the image data from the database
public byte [] getImage(int imageNumber)
{
string strSql = "SELECT * FROM File";
DataSet ds = new DataSet("Image");
OdbcDataAdapter tempAP = new OdbcDataAdapter(strSql,this._objConn);
OdbcCommandBuilder objCommand = new OdbcCommandBuilder(tempAP);
tempAP.Fill(ds,"Table");
try
{
this._objConn.Open();
byte [] buffer = (byte [])ds.Tables["Table"].Rows[imageNumber]["Data"];
return buffer;
}
catch{this._objConn.Close();return null;}
finally{this._objConn.Close();}
}
// Get the image count
public int getCount()
{
string strSql = "SELECT COUNT(Data) FROM File";
DataSet ds = new DataSet("Image");
OdbcDataAdapter tempAP = new OdbcDataAdapter(strSql,this._objConn);
OdbcCommandBuilder objCommand = new OdbcCommandBuilder(tempAP);
tempAP.Fill(ds,"Table");
try>
{
this._objConn.Open();
int count = (int)ds.Tables["Table"].Rows[0][0];
return count;
}
catch{this._objConn.Close();return 0;}
finally{this._objConn.Close();}
}
}
该示例与Odbc一起使用,但是如果您有一些MySQL连接器,则可以使用它.
This sample is with Odbc, but if you have some MySQL Connector you can use it.
这篇关于Mysql上传文件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文