| |

VerySource

 Forgot password?
 Register
Search
View: 989|Reply: 7

C # how to read the BLOB value from SQL. . . Who can teach me. . .

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-10 10:40:01
| Show all posts |Read mode
When I write it, I use
private void button2_Click (object sender, System.EventArgs e)
{
try
{
byte [] photo = GetPhoto (@ "E:\Documents and Settings\wys\My Documents\15.jpg");
SqlConnection con = new SqlConnection ("server = .; uid = sa; pwd =; database = PJ;");
con.Open ();
SqlCommand com = new SqlCommand ("INSERT INTO TABLE1 (edd) Values ​​('" + photo + "')", con);
com.ExecuteNonQuery ();
con.Close ();
}
catch (Exception x)
{
MessageBox.Show (x.Message);
}

}



public static byte [] GetPhoto (string filePath)
{
FileStream fs = new FileStream (filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader (fs);

byte [] photo = br.ReadBytes ((int) fs.Length);

br.Close ();
fs.Close ();
return photo;
}





Who can write the read out picture

I beg you here
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-11 17:00:01
| Show all posts
Why is nobody here? . . Let me push it myself. . Please
Reply

Use magic Report

0

Threads

56

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-1-11 19:27:01
| Show all posts
Read from SqlServer and display it
Add the following code where you need to display the picture:
<asp: image id = "imgPhoto" runat = "server" ImageUrl = "ShowPhoto.aspx"> </ asp: image>

ShowPhoto.aspx body code:
private void Page_Load (object sender, System.EventArgs e)
{
     if (! Page.IsPostBack)
     {
                SqlConnection conn = new SqlConnection ()
                conn.ConnectionString = "Data Source = localhost; Database = test; User Id = sa; Pwd = sa";
                
                string strSql = "select * from test where id = 2"; // Assuming here to get an image with id 2
                SqlCommand cmd = new SqlCommand ()
                reader.Read ();
                Response.ContentType = "application / octet-stream";
                Response.BinaryWrite ((Byte []) reader ["FImage"]);
                Response.End ();
                reader.Close ();
     }
}
Reply

Use magic Report

0

Threads

56

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-1-11 20:36:01
| Show all posts
reader is defined by DataReader
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-18 19:45:01
| Show all posts
Help me see what's wrong. . . How to write if you want to display on Image

CS0234: The type or namespace name "Global" does not exist in the class or namespace "System.Web.UI.WebControls.Image" (are you missing an assembly reference?)
Reply

Use magic Report

0

Threads

10

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-20 12:00:01
| Show all posts
After converting to byte type, it is stored in the Image field.
byte [] imagebytes = null;
FileStream fs = new FileStream (Image_path, FileMode.Open);
BinaryReader br = new BinaryReader (fs);
imagebytes = br.ReadBytes (br.Length);
SqlParameter parInput22 = cmd.Parameters.Add ("@ 员 图片", SqlDbType.Image);
parInput22.Direction = ParameterDirection.Input;
cmd.Parameters ["@ 员 图片"] .Value = imagebytes;
cmd.ExecuteNonQuery ();
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Invalid IP Address

Post time: 2020-8-23 20:30:01
| Show all posts
mark
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-27 20:00:01
| Show all posts
mark
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list