上传到SQL Server数据库脱机

时间:2016-08-27 23:03:06

标签: winforms synchronization offlineapps offline-storage

我试图实现一种方法,当用户将PDF文档上传到SQL Server数据库时。如果网络可用,数据将立即上传,但如果没有互联网连接 - 数据将存储在其他地方,并在互联网连接恢复时上传。请知道如何实现这个目标?

以下是我上传的代码:

//CheckIfNetworkConnected( ) ;
Ping p = new Ping();

try
{
    string host = "www.google.com";
    //bool result = false;

    PingReply reply = p.Send(host, 3000);

    if (reply.Status == IPStatus.Success)
    {
        // Display form modelessly
        pleaseWait.Show();
        //  Allow main UI thread to properly display please wait form.
        Application.DoEvents();

        using (SqlConnection con = new SqlConnection("Server=tcp:mycompany.database.windows.net,1433;Initial Catalog=DBmycompany;Persist Security Info=False;User ID=abubakarkabiru;Password=wordpass123@;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30"))
        {
            FileStream fStream = File.OpenRead(filepath);
            byte[] contents = new byte[fStream.Length];
            fStream.Read(contents, 0, (int)fStream.Length);
            fStream.Close();

            con.Open();
            cmd = new SqlCommand("Insert into document values(@Staffname, @unit, @sites, @docname, @dateofreciept, @descriptions, @doc, @category, @housetype)", con);

            if (textBox1.Visible == true)
            {
                cmd.Parameters.AddWithValue("@Unit", textBox1.Text);
                cmd.Parameters.AddWithValue("@Staffname", textBox2.Text);
            }
            else
            {
                cmd.Parameters.AddWithValue("@Staffname", comboDesigner.SelectedItem.ToString());
                cmd.Parameters.AddWithValue("@unit", comboUnit.SelectedItem.ToString());
            }

            cmd.Parameters.AddWithValue("@category", comboCategory.SelectedItem.ToString());

            if (string.IsNullOrWhiteSpace(txtdocname.Text))
            {
                errorProvider1.SetError(txtdocname, "Compulsory");
            }
            else
            { 
                cmd.Parameters.AddWithValue("@docname", string.IsNullOrWhiteSpace(txtdocname.Text));  
            }

            cmd.Parameters.AddWithValue("@dateofreciept", dateTimePicker1.Value.ToShortDateString());
            cmd.Parameters.AddWithValue("@doc", contents);

            cmd.Parameters.AddWithValue("@sites", string.IsNullOrWhiteSpace(comboSites.ToString())
                           ? (Object)comboSites.SelectedItem.ToString()
                           : (Object)DBNull.Value);

            cmd.Parameters.AddWithValue("@housetype", string.IsNullOrWhiteSpace(comboHouseType.ToString())
                            ? (Object)comboHouseType.SelectedItem.ToString()
                            : (Object)DBNull.Value);                       

            cmd.Parameters.AddWithValue("@descriptions", string.IsNullOrWhiteSpace(txtDesc.Text)
                            ? (object)txtDesc.Text
                            : (object)DBNull.Value);

            var i = cmd.ExecuteNonQuery();

            if (i > 0)
            {
                 MessageBox.Show("Successful");
                 this.notifyIcon1.Icon = this.Icon;
                 this.notifyIcon1.ShowBalloonTip(600, "Upload Notice", "New Document has been Uploaded", ToolTipIcon.Info);
                 //notifyUpload(); 
             }

             con.Close();
         }

         pleaseWait.Hide();
         cleaBoxes();
     }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

0 个答案:

没有答案