2012-12-31 94 views
-1

我想用asp.net从SQL数据库中检索二进制数据,但是输出中显示的数据与我插入的数据不匹配。 这是我的代码:使用asp.net从SQL数据库中检索二进制数据

string EQuery = "SELECT * FROM Ph_Tbl_Contacts WHERE (Contact_ID =" + Contact_ID + ")"; 

DataSet DSs = new DataSet(); 
DataTable dt = new DataTable(); 
DataRow dr = dt.NewRow(); 

DSs = DB.ExecuteQueryData(EQuery); 
dt = DSs.Tables[0]; 

// dr = dt.NewRow(); 
dr = dt.Rows[0]; 
byte[] pic; 
byte[] raw = (byte[])dr["Contact_CardImage"]; 
// Session[OpenDialog.STORED_IMAGE] = raw ; 

,这是嵌件:

byte[] IMAGEbYTE ; 
IMAGEbYTE = (byte[])(Session["SessionImage"]); 
string Query = "INSERT INTO Ph_Tbl_Contacts (Contact_Name, Contact_LName, " + 
       "Contact_Company, Contact_Email, Contact_Tel, " + 
       "Contact_Mobile,Contact_CardImage,Is_Public,User_ID,Save_Date)" + 
       "VALUES (N'" + Txt_Name.Text + "', N'" + Txt_LastName.Text + "', N'" + 
       Txt_CompanyName.Text + "', N'" + Txt_Mail.Text + "', N'" + 
       Txt_Telephone.Text + "', N'" + Txt_Mobile.Text + "','" + 
       IMAGEbYTE + "','" + CheckValue + "'," + 
       Session["User_ID"] + ", N'" + DateStr + "')"; 

DB.ExecuteQueryNoData(Query) ; 
+3

那就是你不使用参数化查询。 – Steve

+0

@helia是否将图像存储为数据库中的二进制[],并且想从中取回它? – Dev

+1

是的,我不知道该怎么做。 –

回答

5

好了,让我们开始清理你的代码。首先要修复你的INSERT代码,因为现在它很容易被SQL注入。您需要使用参数化查询:

using (var conn = new SqlConnection("Your ConnectionString comes here")) 
using (var cmd = conn.CreateCommand()) 
{ 
    conn.Open(); 
    cmd.CommandText = 
    @"INSERT INTO Ph_Tbl_Contacts 
      (Contact_Name, 
      Contact_LName, 
      Contact_Company, 
      Contact_Email, 
      Contact_Tel, 
      Contact_Mobile, 
      Contact_CardImage, 
      Is_Public, 
      User_ID, 
      Save_Date) 
     VALUES 
      (@Contact_Name, 
      @Contact_LName, 
      @Contact_Company, 
      @Contact_Email, 
      @Contact_Tel, 
      @Contact_Mobile, 
      @Contact_CardImage, 
      @Is_Public, 
      @User_ID, 
      @Save_Date) 
    "; 
    cmd.Parameters.AddWithValue("@Contact_Name", Txt_Name.Text); 
    cmd.Parameters.AddWithValue("@Contact_LName", Txt_LastName.Text); 
    cmd.Parameters.AddWithValue("@Contact_Company", Txt_CompanyName.Text); 
    cmd.Parameters.AddWithValue("@Contact_Email", Txt_Mail.Text); 
    cmd.Parameters.AddWithValue("@Contact_Tel", Txt_Telephone.Text); 
    cmd.Parameters.AddWithValue("@Contact_Mobile", Txt_Mobile.Text); 
    cmd.Parameters.AddWithValue("@Contact_CardImage", IMAGEbYTE); 
    cmd.Parameters.AddWithValue("@Is_Public", CheckValue); 
    cmd.Parameters.AddWithValue("@User_ID", Session["User_ID"]); 
    cmd.Parameters.AddWithValue("@Save_Date", DateStr); 
    cmd.ExecuteNonQuery(); 
} 

这里值得一提的是,如果在你的数据库中的Save_Date列是一个datetime你应该为参数传递的DateTime一个实例,并且不试图将其转换为字符串=>DateStr必须是DateTime。

好了,现在你已经正确插入记录到数据库中,你可以阅读:

using (var conn = new SqlConnection("Your ConnectionString comes here")) 
using (var cmd = conn.CreateCommand()) 
{ 
    conn.Open(); 
    cmd.CommandText = 
    @"SELECT 
      Contact_CardImage 
     FROM 
      Ph_Tbl_Contacts 
     WHERE 
      Contact_ID = @Contact_ID 
    "; 
    cmd.Parameters.AddWithValue("@Contact_ID", Txt_Name.Text); 
    using (var reader = cmd.ExecuteReader()) 
    { 
     if (reader.Read()) 
     { 
      byte[] raw = (byte[])reader.Items["Contact_CardImage"]; 
      // TODO: do something with the raw data 
     } 
    } 
} 
+2

谢谢,但我有这个错误:错误'System.Data.SqlClient.SqlDataReader'没有包含'Items'的定义,也没有接受类型为“System.Data.SqlClient”的第一个参数的扩展方法'Items'。 SqlDataReader'可以找到(你是否缺少使用指令或程序集引用?) –

+1

非常感谢它的工作原理 –