开发者

Retrieving VARCHAR(MAX)

How can I retrieve a colum开发者_如何学Pythonn of datatype Varchar(MAX) from a SQL Server database in C#?

I don't think SqlDataReader helps retrieve it.

Any clue?

Thanks


It's just a string field .... just grab it like any other string.....

// define your query
string query = "SELECT YourField FROM dbo.YourTable WHERE ID = 1";

using(SqlConnection conn = new SqlConnection("......"))
using(SqlCommand cmd = new SqlCommand(query, conn))
{
   conn.Open();

   using(SqlDataReader rdr = cmd.ExecuteReader())
   {
       if(rdr.Read())
       {
          string fieldValue = rdr.GetString(0);
       }
   }

   conn.Close();
}

There's really nothing special about the VARCHAR(MAX) type - it's just a text field, and it can hold up to 2 GB of text - just like a .NET string. You don't need to do anything special, like streaming or anything - just read the column into a string!


Working with Large-Value Types in ADO.NET

reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read())
{
    SqlChars buffer = reader.GetSqlChars(0);
}

Or just convert it to CLR type:

while (reader.Read())
{
     string str = reader[0].ToString();
     Console.WriteLine(str);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜