从SQL BASE C#中选择IMAGE [英] Select IMAGE from SQL BASE C#

查看:57
本文介绍了从SQL BASE C#中选择IMAGE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码正常工作。



sql中图像的列名是数据。



In这段代码我需要把图片格式数据库放到图片盒里



一些帮助



什么我试过了:



This code working fine.

Column name for image in sql is data.

In this code I need to put inside picture form database to picturebox

Some help

What I have tried:

private void prijavaAction()  //04.04. final doradjena perfect
       {

           {
               SqlConnection con = new SqlConnection(cs);

               if (textBox1.Text.All(char.IsDigit))

               {
                   string queryString = "SELECT [ime_prezime] FROM dbo.radnici WHERE bar_kod = '" + textBox1.Text + "'";// pronaci radnika u bazi
                   using (SqlConnection connection = new SqlConnection(cs))
                   {
                       SqlCommand command = new SqlCommand(queryString, connection);
                       connection.Open();
                       SqlDataReader reader = command.ExecuteReader();



                       string commanda = "select case when (SELECT (DATEDIFF(SECOND, Max(vrijemeprijave), GETDATE()) / 60) from dbo.prijava_radnika where status='prijavljen' and radnik= (" + queryString + ")) < 5 THEN 1 ELSE 0 END";
                       SqlConnection connection3 = new SqlConnection(cs);
                       SqlCommand command2 = new SqlCommand(commanda, connection3);
                       connection3.Open();

                       int broj = Convert.ToInt32(command2.ExecuteScalar());



                       string commanda2 = "select case when (SELECT (DATEDIFF(SECOND, Max(vrijemeodjave), GETDATE()) / 60) from dbo.prijava_radnika where status='odjavljen' and radnik= (" + queryString + ")) < 5 THEN 1 ELSE 0 END";
                       SqlConnection connection4 = new SqlConnection(cs);
                       SqlCommand command4 = new SqlCommand(commanda2, connection4);
                       connection4.Open();

                       int broj2 = Convert.ToInt32(command4.ExecuteScalar());

                       if (broj == 1 || broj2 == 1)

                       {
                           notificationPanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#FD7400");
                           messageLabel.Text = "Prijava i odjava su blokirani 5 minuta \nza ovog radnika";
                           textBox1.Text = "";
                       }
                       else
                       {




                           try
                           {
                               if (reader.Read())

                               {

                                   string prijava = "SELECT count(*) from dbo.prijava_radnika where vrijemeprijave is not null and vrijemeodjave is null and radnik = '" + readerresultTextbox.Text + "' and redni_broj is NULL";
                                   SqlCommand provjeraprijaveradnika = new SqlCommand(prijava, con);
                                   con.Open();
                                   int result = Convert.ToInt32(provjeraprijaveradnika.ExecuteScalar());

                                   con.Close();
                                   if ((result == 0))

                                   {
                                       String saveStaff = "INSERT INTO dbo.prijava_radnika (vrijemeprijave, status, radnik,idfakture) VALUES(@vrijemeprijave,'prijavljen', '" + readerresultTextbox.Text + "',@idfakture)";
                                       using (SqlConnection openCon2 = new SqlConnection(cs))
                                       {
                                           using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))
                                           {
                                               querySaveStaff.Connection = openCon2;
                                               querySaveStaff.Parameters.Add("@vrijemeprijave", SqlDbType.DateTime).Value = DateTime.Now;
                                               querySaveStaff.Parameters.Add("@idfakture", SqlDbType.Int).Value = id_fakture;
                                               openCon2.Open();
                                               querySaveStaff.ExecuteNonQuery();
                                               openCon2.Close();

                                               notificationPanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#2EFF36");
                                           //  messageLabel.Text = (reader["ime_prezime"].ToString()) + " je prijavljen u \n " + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss");
                                               messageLabel.Text = ("Dobro došli na posao...\n" + reader["ime_prezime"].ToString()) + " \nPrijavljeni ste od\n " + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss");
                                               readerresultTextbox.Text = (reader["ime_prezime"].ToString());
                                           }
                                       }
                                   }
                                   else
                                   {

                                       String updateStaff = " declare @maxNo integer = 0 select @maxNo = isnull(max(redni_broj), 0) from[dbo].[prijava_radnika] UPDATE[dbo].[prijava_radnika] SET"
                                           + " [vrijemeodjave] = @vrijemeodjave,[redni_broj] = (@maxNo+1),[status] = 'odjavljen' WHERE radnik = '" + readerresultTextbox.Text + "'and [status] = 'prijavljen'";



                                       using (SqlConnection connection1 = new SqlConnection(cs))
                                       {
                                           SqlCommand command1 = new SqlCommand(queryString, connection1);
                                           connection1.Open();
                                           SqlDataReader reader1 = command1.ExecuteReader();
                                           connection.Close();
                                           try
                                           {
                                               if (reader1.Read())
                                               {


                                                   using (SqlConnection openCon3 = new SqlConnection(cs))
                                                   {
                                                       using (SqlCommand queryupdateStaff = new SqlCommand(updateStaff))
                                                       {
                                                           queryupdateStaff.Connection = openCon3;
                                                           queryupdateStaff.Parameters.Add("@vrijemeodjave", SqlDbType.DateTime).Value = DateTime.Now;
                                                           openCon3.Open();
                                                           queryupdateStaff.ExecuteNonQuery();
                                                           openCon3.Close();

                                                       }
                                                   }

                                                   notificationPanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFE11A");
                                                   messageLabel.Text = (reader1["ime_prezime"].ToString()) + " je odjavljen u \n " + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss");
                                               }
                                           }
                                           catch { }


                                       }



                                       reader.Close();
                                   }

                               }
                               else
                               {
                                   notificationPanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#FD7400");
                                   messageLabel.Text = "Radnik ne postoji u bazi! \nProverite vašu karticu ili probajte ponovo!";
                                   textBox1.Text = "";


                               }
                           }
                           finally
                           {

                           }
                       }



                   }
               }

               else
               {
                   notificationPanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#FD7400");
                   messageLabel.Text = "Bar kod nije važeci ili nije bar kod";
                   textBox1.Text = "";
               }





           }
       }

推荐答案

您必须查看调试器,但通常图像数据的数据类型是 byte [] 。所以,你的问题是从 byte [] 转换为 Image 。如果是这样,下面的内容应该可以解决问题:



You'll have to look in the debugger, but generally the data type for image data is byte[]. So, you're problem is to convert from byte[] to Image. If so, something like the following should do the trick:

using (var stream = new MemoryStream((byte[])reader4["data"]))
  pictureBox.Image = Image.FromStream(stream);





祝你好运,希望有所帮助。



Good luck, hope that helps.


已经指出你需要以二进制格式处理数据。有关完整示例,请查看 c# - 从sql server数据库中检索图像 - DeveloperQ - 开发者问题网 - 海量问题解决方案 [ ^ ]



其他问题

- 您应该在查询中使用参数。正如所指出的那样,当将字符串连接到SQL语句时,你最终会遇到问题

- 空的catch块,如果发生错误,则省略所有异常,因为你有一个空的catch块。您至少应该向用户显示一条消息,告知执行不成功

- 多个连接。看起来你在执行过程中打开了多个连接,为什么?这只会增加不必要的代码并减慢程序的速度
As already pointed out you need to handle the data in binary format. For a complete example, have a look at c# - Retrieve Images from sql server database - Stack Overflow[^]

Other issues
- You should use parameters with your queries. As pointed out you're going to end up with problems when just concatenating strings into the SQL statement
- Empty catch block, if an error occurs you omit all exceptions because you have an empty catch block. You should at least show a message to the user to inform about unsuccessful execution
- Multiple connections. It looks like you're opening multiple connections during the execution, why? This only adds unnecessary code and slows down the program


最后我添加但是我有错误



On finally I add but I have error

Error CS0029 Cannot implicitly convert type 'string' to 'System.Drawing.Image' 







string slika = "SELECT [data] FROM dbo.radnici WHERE ime_prezime = '" + readerresultTextbox.Text + "'";// pronaci radnika u bazi
using (SqlConnection connection2 = new SqlConnection(cs))
{
    SqlCommand command3 = new SqlCommand(slika, connection2);
    connection2.Open();
    SqlDataReader reader4 = command3.ExecuteReader();
    connection2.Close();
    if(reader4.Read())
    {
        pictureBox.Image = (reader4["data"].ToString());
    }
    else
    {
        //
    }
}


这篇关于从SQL BASE C#中选择IMAGE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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