以下内容转载于http://www.cnblogs.com/greatqn/archive/2007/03/21/637117.html,
希望楼主,不会介意,我已经注明来源了
在存贮过程里读写text,要用到READTEXT UPDATETEXT WRITETEXT 进行操作.
在C#中,用select text from table 是可以直接取到值的.
在做更新,新增操作时,用@text传text的值,也能正常赋值.
text类型直接赋string
image类型用byte[]
insert:
string
strSql
=
"
INSERT INTO [dbo].[Tableimg]( [img]) VALUES(@img)
"
;
using (System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(CommConfig.InfodbConnectstring))
{
System.Data.SqlClient.SqlCommand myCommand = new System.Data.SqlClient.SqlCommand(strSql, myConnection);
myConnection.Open();
string str = new string('X', 100000);
byte[] bytes = System.Text.Encoding.Default.GetBytes(str.ToCharArray());
SqlParameter sp = new SqlParameter("@img", SqlDbType.Image);
sp.Value = bytes;
myCommand.Parameters.Add(sp);
int re = myCommand.ExecuteNonQuery();
myCommand.Dispose();
myConnection.Close();
}
using (System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(CommConfig.InfodbConnectstring))
{
System.Data.SqlClient.SqlCommand myCommand = new System.Data.SqlClient.SqlCommand(strSql, myConnection);
myConnection.Open();
string str = new string('X', 100000);
byte[] bytes = System.Text.Encoding.Default.GetBytes(str.ToCharArray());
SqlParameter sp = new SqlParameter("@img", SqlDbType.Image);
sp.Value = bytes;
myCommand.Parameters.Add(sp);
int re = myCommand.ExecuteNonQuery();
myCommand.Dispose();
myConnection.Close();
}
select:
string
strSql
=
"
SELECT [id], [img] FROM [dbo].[Tableimg] where id = 1
"
;
using (System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(CommConfig.InfodbConnectstring))
{
System.Data.SqlClient.SqlCommand myCommand = new System.Data.SqlClient.SqlCommand(strSql, myConnection);
myConnection.Open();
SqlDataReader dr = myCommand.ExecuteReader();
while (dr.Read())
{
Byte[] bytes = (byte[])dr[1];
string str = System.Text.Encoding.Default.GetString(bytes);
}
myCommand.Dispose();
myConnection.Close();
}
using (System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(CommConfig.InfodbConnectstring))
{
System.Data.SqlClient.SqlCommand myCommand = new System.Data.SqlClient.SqlCommand(strSql, myConnection);
myConnection.Open();
SqlDataReader dr = myCommand.ExecuteReader();
while (dr.Read())
{
Byte[] bytes = (byte[])dr[1];
string str = System.Text.Encoding.Default.GetString(bytes);
}
myCommand.Dispose();
myConnection.Close();
}
此文转载于http://www.cnblogs.com/greatqn/archive/2007/03/21/637117.html