SQL - 我的图片正确保存到数据库

时间:2017-11-06 14:51:44

标签: sql sql-server

是我的图片正确保存到数据库,因为我无法加载它们。

take a look at this image

在图像中很少有字节似乎保存,我想它们没有正确保存,因为我无法将它们收回:(

    public byte[] ImageToByte(Image img)
    {
        MemoryStream ms = new MemoryStream();
        img.Save(ms, img.RawFormat);
        return ms.ToArray();
    }

我使用它将图像转换为字节,然后我将其保存在db

        SqlConnection MyCon = new SqlConnection("Data Source=DESKTOP-3DH5S38\\HR_SERVER;Initial Catalog=BMS_PRO_DB;Integrated Security=True");

    private void CreateAccount_Btn_Click(object sender, EventArgs e)
    {
        MyCon.Open();
        string query = "INSERT INTO tbl_Bank_Accounts_Information (CustomerName,AccountNo,AccountTitle,AccountType,Gender,DOB,Nationality,PostalAddress,PhoneNumber,NicNumber,EmailAddress,CompanyName,Occupation,AccountBalance,ProfilePic,SignaturePic) VALUES('" + CustomerName_textBox.Text+"' , '"+AccountNumber_textBox.Text+"' , '"+AccountTitle_textbox .Text+"' , '"+AccountType_ComboBox.Text + "' , '" + WhichGender + "' , '" + MyDateTimePicker.Text + "' , '" +Nationality_ComboBox.Text + "' , '" +PostalAddress_textbox.Text + "' , '" + (PhoneNo_ComboBox.Text+PhoneNo_textBox.Text) + "' , '" + NicNumber_textbox.Text+ "' , '" +(EmailAddress_textbox.Text+EmailAddress_atComboBox.Text) + "' , '" +CompanyName_textbox.Text + "' , '" +Occupation_textbox.Text + "' , '" +InitialDeposit_textbox.Text +"' , '"+ Pb_SetObj.ImageToByte(Profile_Picture_PictureBox.Image) + "', '" + Pb_SetObj.ImageToByte(Signature_PictureBox.Image) + "')";
        SqlDataAdapter dataAdapter = new SqlDataAdapter(query, MyCon);
        dataAdapter.SelectCommand.ExecuteNonQuery();
        MyCon.Close();
        MessageBox.Show("Account Successfully Created");
    }

2 个答案:

答案 0 :(得分:2)

不,图像未正确保存。您似乎正在存储类型名称的字符串值而不是字节数组,如下面的查询所示。

如果您需要帮助存储二进制值,请发布您的问题代码。

SELECT CAST(0X53797374656D2E427974655B5D AS varchar(10));

结果:

System.Byte[]

修改

以下是参数化查询示例。我对你的数据库类型和长度进行了猜测。您应该更改参数类型和最大长度规范以匹配实际的表架构,并使用字符串类型的值设置值(例如DateTime而不是String)。

使用SqlDataAdapter与原始代码一样没有任何价值,我发现使用SelectCommand进行插入会让人感到困惑。

请注意,这假设您的Pb_SetObj方法必须返回图像的字节数组;

using (var MyCon = new SqlConnection(@"Data Source=.;Initial Catalog=tempdb;Integrated Security=SSPI"))
{
    MyCon.Open();

    string query =
        @"INSERT INTO tbl_Bank_Accounts_Information (CustomerName,AccountNo,AccountTitle,AccountType,Gender,DOB,Nationality,PostalAddress,PhoneNumber,NicNumber,EmailAddress,CompanyName,Occupation,AccountBalance,ProfilePic,SignaturePic)
    VALUES(@CustomerName,@AccountNo,@AccountTitle,@AccountType,@Gender,@DOB,@Nationality,@PostalAddress,@PhoneNumber,@NicNumber,@EmailAddress,@CompanyName,@Occupation,@AccountBalance,@ProfilePic,@SignaturePic);";

    var insertCommand = new SqlCommand(query, MyCon);

    insertCommand.Parameters.Add("@CustomerName", SqlDbType.VarChar, 100).Value = CustomerName_textBox.Text;
    insertCommand.Parameters.Add("@AccountNo", SqlDbType.VarChar, 100).Value = AccountNumber_textBox.Text;
    insertCommand.Parameters.Add("@AccountTitle", SqlDbType.VarChar, 100).Value = AccountTitle_textbox.Text;
    insertCommand.Parameters.Add("@AccountType", SqlDbType.VarChar, 100).Value = AccountType_ComboBox.Text;
    insertCommand.Parameters.Add("@Gender", SqlDbType.VarChar, 100).Value = WhichGender;
    insertCommand.Parameters.Add("@DOB", SqlDbType.Date).Value = MyDateTimePicker.Value;
    insertCommand.Parameters.Add("@Nationality", SqlDbType.VarChar, 100).Value = Nationality_ComboBox.Text;
    insertCommand.Parameters.Add("@PostalAddress", SqlDbType.VarChar, 100).Value = PostalAddress_textbox.Text;
    insertCommand.Parameters.Add("@PhoneNumber", SqlDbType.VarChar, 100).Value = PhoneNo_ComboBox.Text + PhoneNo_textBox.Text;
    insertCommand.Parameters.Add("@NicNumber", SqlDbType.VarChar, 100).Value = NicNumber_textbox.Text;
    insertCommand.Parameters.Add("@EmailAddress", SqlDbType.VarChar, 100).Value = EmailAddress_textbox.Text + EmailAddress_atComboBox.Text;
    insertCommand.Parameters.Add("@CompanyName ", SqlDbType.VarChar, 100).Value = CompanyName_textbox.Text;
    insertCommand.Parameters.Add("@Occupation ", SqlDbType.VarChar, 100).Value = Occupation_textbox.Text;
    insertCommand.Parameters.Add("@AccountBalance", SqlDbType.VarChar, 100).Value = InitialDeposit_textbox.Text;
    insertCommand.Parameters.Add("@ProfilePic", SqlDbType.VarBinary, -1).Value = Pb_SetObj.ImageToByte(Profile_Picture_PictureBox.Image);
    insertCommand.Parameters.Add("@SignaturePic", SqlDbType.VarBinary, -1).Value = Pb_SetObj.ImageToByte(Signature_PictureBox.Image);

    insertCommand.ExecuteNonQuery();
    MyCon.Close();

}

答案 1 :(得分:0)

我努力了,最后我成功了!

 SqlConnection MyCon = new SqlConnection("server=DESKTOP-3DH5S38123\\HR2_SERVER;database=BMS_PRO_DB;Integrated Security =true");
            string query = @"INSERT INTO tbl_Bank_Accounts_Information (CustomerName,AccountNo,AccountTitle,AccountType,Gender,DOB,Nationality,PostalAddress,PhoneNumber,NicNumber,EmailAddress,CompanyName,Occupation,AccountBalance,ProfilePic,SignaturePic) VALUES(@CustomerName,@AccountNo,@AccountTitle,@AccountType,@Gender,@DOB,@Nationality,@PostalAddress,@PhoneNumber,@NicNumber,@EmailAddress,@CompanyName,@Occupation,@AccountBalance,@ProfilePic,@SignaturePic);";
            SqlCommand comnd = new SqlCommand(query, MyCon);
            MyCon.Open();
            comnd.Parameters.AddWithValue("@CustomerName", CustomerName_textBox.Text);
            comnd.Parameters.AddWithValue("@AccountNo", AccountNumber_textBox.Text);
            comnd.Parameters.AddWithValue("@AccountTitle", AccountTitle_textbox.Text);
            comnd.Parameters.AddWithValue("@AccountType", AccountType_ComboBox.Text);
            comnd.Parameters.AddWithValue("@Gender", WhichGender);
            comnd.Parameters.AddWithValue("@DOB", MyDateTimePicker.Text);
            comnd.Parameters.AddWithValue("@Nationality", Nationality_ComboBox.Text);
            comnd.Parameters.AddWithValue("@PostalAddress", PostalAddress_textbox.Text);
            comnd.Parameters.AddWithValue("@PhoneNumber", (PhoneNo_ComboBox.Text + PhoneNo_textBox.Text));
            comnd.Parameters.AddWithValue("@NicNumber", NicNumber_textbox.Text);
            comnd.Parameters.AddWithValue("@EmailAddress", (EmailAddress_textbox.Text + EmailAddress_atComboBox.Text));
            comnd.Parameters.AddWithValue("@CompanyName", CompanyName_textbox.Text);
            comnd.Parameters.AddWithValue("@Occupation", Occupation_textbox.Text);
            comnd.Parameters.AddWithValue("@AccountBalance", InitialDeposit_textbox.Text);
            comnd.Parameters.AddWithValue("@ProfilePic", Pb_SetObj.ImageToByte(Profile_Picture_PictureBox.Image));
            comnd.Parameters.AddWithValue("@SignaturePic", Pb_SetObj.ImageToByte(Signature_PictureBox.Image));
            comnd.ExecuteNonQuery();
            MyCon.Close();

Dan Guzman的解决方案字面上给了我一个充满错误的页面,但我选择了他的答案,因为我的问题是"我的照片是否正确保存到数据库"他老老实实地回答。