如何在将XML文件导入SQLServerManagementStudio时为存储过程创建进度条
本文关键字:存储过程 创建 SQLServerManagementStudio XML 导入 文件 | 更新日期: 2023-09-27 17:57:49
我被卡住了:这是我的示例代码
namespace WindowsFormsApplicationXml
{
public partial class Form1 : Form
{
string ConString, CmdString;
int TotalRecords;
SqlConnection con;
SqlCommand cmd;
SqlDataReader reader;
public Form1()
{
InitializeComponent();
ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
}
public class MyWorkerClass
{
public int UserID;
}
private void btnSaveXML_Click(object sender, EventArgs e)
{
MyWorkerClass obj = new MyWorkerClass();
if (!backgroundWorker1.IsBusy)
{
TotalRecords = GetTotalRecords();
progressBar1.Maximum = TotalRecords;
backgroundWorker1.RunWorkerAsync(obj);
}
}
private int GetTotalRecords()
{
try
{
using (con = new SqlConnection(ConString))
{
cmd = new SqlCommand("SELECT COUNT(UserID) FROM UserMaster", con);
con.Open();
TotalRecords = int.Parse(cmd.ExecuteScalar().ToString());
con.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return TotalRecords;
}
public void LoadData(MyWorkerClass obj)
{
try
{
lblMessage.Text = "Please, Wait importing data from XML is in progress....";
lblMessage.Visible = true;
btnSaveXML.Enabled = false;
DataSet ds = new DataSet();
XmlTextReader xmlreader = new XmlTextReader(txtXMLFilePath.Text.ToString());
xmlreader.MoveToContent();
xmlreader.Read();
ds.ReadXml(xmlreader);
xmlreader.Close();
if (ds.Tables.Count != 0)
{
using (SqlConnection con = new SqlConnection("Data Source= YOUNGTECH-PC''SQLEXPRESSNEW;Integrated Security=true;Initial Catalog=CADirect_CT; uid=reshav; Password=kanak; "))
{
string xml = ds.GetXml();//for verification code
con.Open();
SqlCommand cmd = new SqlCommand("sp_ct_importUserXml", con);
cmd.CommandType = CommandType.StoredProcedure;
int i = 0;
cmd.Parameters.Add("@XMLdata", SqlDbType.Xml).Value = ds.GetXml();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds1 = new DataSet();
da.Fill(ds1);
lblResulttext.Visible = true;
dgviewXMLData.Visible = true;
dgviewXMLData.DataSource = ds1.Tables[0].DefaultView;
AutoNumberRowsForGridView(dgviewXMLData);
//i++;
//Thread.Sleep(10);
con.Close();
lblMessage.Text = "Successfully import XML data with below result.";
btnSaveXML.Enabled = true;
}
}
}
catch (Exception ex)
{
lblMessage.Visible = true;
lblMessage.Text = ex.Message;
}
}
public void AutoNumberRowsForGridView(DataGridView dataGridView)
{
if (dataGridView != null)
{
for (int count = 0; (count <= (dataGridView.Rows.Count - 1)); count++)
{
dataGridView.Rows[count].HeaderCell.Value = string.Format((count + 1).ToString(), "0");
}
}
}
private void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
// To list only xml files, we need to add this filter
openFileDialog.Filter = "|*.xml";
DialogResult result = openFileDialog.ShowDialog();
if (result == DialogResult.OK)
{
txtXMLFilePath.Text = openFileDialog.FileName;
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
MyWorkerClass obj = (MyWorkerClass)e.Argument;
LoadData(obj);
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
if (!backgroundWorker1.CancellationPending)
{
MyWorkerClass obj = (MyWorkerClass)e.UserState;
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
}
}
您可以定义一个控制器子动作,该动作可以根据导入的数据量返回条形图的值(0-100)。
使用ajax调用呈现进度条,每500毫秒一次:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Title</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js"></script>
</head>
<body>
<div id="progressbar"></div>
<script >
//Calls function every 3 seconds
$(function()
{
$("#progressbar").progressbar(
{
value: 0
});
});
window.setInterval(function()
{
window.progressBarValue = 0;
if(window.progressBarValue < 100)
{
$.ajax(
{
//I'm using this to get a random int, you should create a controller action which should return the data status
url: "http://www.random.org/integers/?num=1&min=1&max=100&col=1&base=10&format=plain&rnd=new", //"@Url.Action("YourGetProgressBarAction","YourControllerName")",
success:function(data)
{
//data should be a value like 37, 53, 87
$(function()
{
window.progressBarValue = parseInt(data);
$("#progressbar").progressbar(
{
value: parseInt(data)
});
});
}}
);
}
},500);
</script>
</body>
</html>
关于如何获取导入了多少XML,这取决于您的操作方式。最简单的方法是计算XML中有多少对象,并在每次成功插入数据库后更新进度条。
通过这种方式,您可以调用方法来估计您在控制器操作中导入的量(将总量存储在变量中,并通过属性或方法使其可用于控制器)。通过了解XML中有多少对象以及导入了多少对象,可以应用三规则来确定进度条状态。
对于您正在使用的编码系统来说,这似乎是一个很好的答案:asp.net 中的Progressbar
查看标记的答案;标记答案中的按钮将更改为用于启动导入XML过程的任何控件。ASP.Net UpdateProgress控件是在这里做工作的,请务必阅读MSDN上的控件。