locked
how to scan a document using c# .net RRS feed

  • Question

  • User1046245955 posted

    I am planning in making an application (Archiving System specifically) wherein, my app can access scanner for scanning of document and storing it to database and/or server. Any suggestions? Just a simple scan document -> store to database type of solution will do. Thanks! :)  i prefer c# .net 

    Sunday, February 24, 2019 1:02 PM

All replies

  • User-1174608757 posted

    Hi paminchever ,

    According to your description, I suggest you could use fileupload control to choose one file ,and transfer it as binary byte stream then you could save the stream in the database.

    I have made a sample here. I hope it could help you.

    Aspx

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:FileUpload ID="FileUpload1" runat="server" />
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            </div>
        </form>
    </body>
    </html>
    

    aspx,cs:

      public partial class fileupload : System.Web.UI.Page
        {
            private static readonly string conStr = ConfigurationManager.ConnectionStrings["mssqlserver"].ConnectionString;
    
            public static int ExecuteNonQuery(string sql, params SqlParameter[] pms)
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    using (SqlCommand cmd = new SqlCommand(sql, con))
                    {
                        if (pms != null)
    
                        {
    
                            cmd.Parameters.AddRange(pms);
                        }
                        con.Open();
                        return cmd.ExecuteNonQuery();
                    }
    
    
                }
    
    
            }
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                using (Stream fs = FileUpload1.PostedFile.InputStream)
                {
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        byte[] bytes = br.ReadBytes((Int32)fs.Length); // transfer the file to bytes
                        string sql = "insert into Fight values('3',@data) ";
                        SqlParameter prm = new SqlParameter("@data", bytes);
                        ExecuteNonQuery(sql,prm);
                        Response.Write("insert successfully");
                    }
    
                }
    
            }
        }

    It shows as below:

    In sqlserver table, you could see:

    Best Regards

    Wei Zhang

    Monday, February 25, 2019 2:34 AM