我已经为c#创建了一个图像插入和检索代码?使用此代码可以将多个图像插入数据库并检索多个图像吗? [英] I have created an image insertion and retrieve codes for c#? using this code can i insert multiple images to database and retrieve multiple images?

查看:55
本文介绍了我已经为c#创建了一个图像插入和检索代码?使用此代码可以将多个图像插入数据库并检索多个图像吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

----图像插入----



 SqlConnection conn = 新的 SqlConnection(); 
conn.ConnectionString = 数据源= PROMOD-PC;初始目录= travel_Directions;集成安全性=真 ;

String Strt_Address = TextBox1.Text;
String End_Address = TextBox2.Text;

String filePath = FileUpload1.PostedFile.FileName;
String filename = Path.GetFileName(filePath);
String ExtStr = Path.GetExtension(filename);
String contenttype = String .Empty;

switch (ExtStr)
{
case 。png
contenttype = image / png;
break ;

case .jpg
contenttype = image / jpg;
break ;

case 。gif
contenttype = image / gif;
break ;
}

if (contenttype!= string .Empty)
{
Stream Strmf = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(Strmf);
Byte [] imgbytes = br.ReadBytes(( Int32 )Strmf.Length);

conn.Open();

字符串 QueryStr = UPDATE MapDataImage SET Image = @ Image WHERE Source =' + TextBox1.Text + ';< /跨度>;
SqlCommand scmd = new SqlCommand(QueryStr,conn);
scmd.Parameters.Add( @ Image,SqlDbType.VarBinary).Value = imgbytes;
scmd.ExecuteNonQuery();







这里我使用插入查询..然后我可以添加多个图像...当我测试查询时,它会显示不同的图像。

基本上我想用我的数据库检查文本框...我的值是否正确。

如果它是正确的我想给用户上传无限量照片的能力



----图像检索----





SqlConnection con = new SqlConnection();

con.ConnectionString =Data Source = PROMOD-PC; Initial Catalog = travel_Directions ;集成安全性=真;





DataTable dt = new DataTable();



String selQuery =SELECT Image FROM MapDataImage WHERE Source ='+ TextBox1.Text +';;



 SqlCommand SelCmm nd =  new  SqlCommand(selQuery); 
SqlDataAdapter sDatAdp = new SqlDataAdapter();
SelCmmnd.CommandType = CommandType.Text;
SelCmmnd.Connection = con;

con.Open();

SqlDataReader dr = SelCmmnd.ExecuteReader();
dr.Read();
Context.Response.BinaryWrite(( byte [])dr [ 图像]);
Context.Response.ContentType = image / jpg;



sDatAdp.SelectCommand = SelCmmnd;


// sDatAdp.Fill(dr);

GridView1.DataSource = dr;
GridView1.DataBind();

con.Close();
dr.Close();
sDatAdp.Dispose();
con.Dispose();
dt.Dispose();





所以基本上我需要检索多张图片



i尝试使用listview和gridview

它给了我数据绑定错误



我不能用两个数据库做到这一点吗?比如

第一个数据库数据库和第二个数据库....

就好像你的第一个表的数据还好...然后它转到第二个表添加图像。



当我检索那些我可以用于循环查看的图像?

是否正确。

解决方案

试试这个...... :)



使用ASP.Net从SQL Server数据库保存和检索文件 [ ^

---- Image Insertion ----

SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source=PROMOD-PC;Initial Catalog=travel_Directions;Integrated Security=True";
    
            String Strt_Address = TextBox1.Text;
            String End_Address = TextBox2.Text;
    
            String filePath = FileUpload1.PostedFile.FileName;
            String filename = Path.GetFileName(filePath);
            String ExtStr = Path.GetExtension(filename);
            String contenttype = String.Empty;
    
            switch (ExtStr)
            { 
                case ".png":
                    contenttype = "image/png";
                    break;
    
                case ".jpg":
                    contenttype = "image/jpg";
                    break;
    
                case ".gif":
                    contenttype = "image/gif";
                    break;
            }
    
            if (contenttype != string.Empty)
            {
            Stream Strmf = FileUpload1.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(Strmf);
                Byte[] imgbytes = br.ReadBytes((Int32)Strmf.Length);
                
                conn.Open();
    
                String QueryStr = "UPDATE MapDataImage SET Image = @Image WHERE Source='" + TextBox1.Text + "';";
                SqlCommand scmd = new SqlCommand(QueryStr, conn);
                scmd.Parameters.Add("@Image", SqlDbType.VarBinary).Value = imgbytes;
                scmd.ExecuteNonQuery();




Here i used insert queries before.. then i can add multiple images... and when i test queries it will shows image differantly.
basically i want to check textbox with my database... wheather my values are correct or not.
if it's correct i want to give user ability to upload unlimited photos

---- Image Retrieve ----


SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=PROMOD-PC;Initial Catalog=travel_Directions;Integrated Security=True";


DataTable dt = new DataTable();

String selQuery = "SELECT Image FROM MapDataImage WHERE Source='" + TextBox1.Text + "';";

SqlCommand SelCmmnd = new SqlCommand(selQuery);
    SqlDataAdapter sDatAdp = new SqlDataAdapter();
    SelCmmnd.CommandType = CommandType.Text;
    SelCmmnd.Connection = con;

    con.Open();

    SqlDataReader dr = SelCmmnd.ExecuteReader();
    dr.Read();
    Context.Response.BinaryWrite((byte[])dr["Image"]);
    Context.Response.ContentType = "image/jpg";



    sDatAdp.SelectCommand = SelCmmnd;


    //sDatAdp.Fill(dr);

    GridView1.DataSource = dr;
    GridView1.DataBind();

    con.Close();
    dr.Close();
    sDatAdp.Dispose();
    con.Dispose();
    dt.Dispose();



so basically i need to retrieve multiple images as well

i tried with listview and gridview
it gives me databind error

can't i do it with two data bases? like
first database for data and 2nd one for images....
like if your first table's data okay... then it goes to 2nd table to add the image.

when i retrieve those images i can use for loop to view that?
is that correct or not.

解决方案

try this...:)

Save and Retrieve Files from SQL Server Database using ASP.Net[^]


这篇关于我已经为c#创建了一个图像插入和检索代码?使用此代码可以将多个图像插入数据库并检索多个图像吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆