The following content is reproduced in http://www.cnblogs.com/greatqn/archive/2007/03/21/637117.html,
I hope the landlord will not mind. I have already indicated the source.
To read and write text during storage, you must use readtext updatetext writetext for operations.
In C #, values can be obtained directly using select text from table.
During the update and add operations, use @ text to pass the text value, and assign values normally.
String is directly assigned to the text type.
Byte [] for the image type
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 ();
}
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 ();
}
This article is reprinted in http://www.cnblogs.com/greatqn/archive/2007/03/21/637117.html