using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace SqlProcedureCall
{
delegate void InvokeExec();
public partial class Form1 : Form
{
public static string conn = "server=tripodcn-test;uid=sa;pwd=123456;database=R2_sql1";
public Thread[] T = new Thread[10];
public GetSQLResponseRoute[] Route = new GetSQLResponseRoute[10];
public Form1()
{
InitializeComponent();
Form.CheckForIllegalCrossThreadCalls = false;
}
private void button1_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
for (int i = 0; i <10; i++)
{
Route[i] = new GetSQLResponseRoute(this, "test", i);
T[i] = new Thread(new ThreadStart(Route[i].GetSQLResponse));
T[i].Start();
}
}
public class GetSQLResponseRoute {
Form1 f1;
string param1;
int param2;
public GetSQLResponseRoute(Form1 f1, string param1, int param2)
{
this.f1 = f1;
this.param1 = param1;
this.param2 = param2;
}
public void GetSQLResponse() {
SqlConnection sqlconn = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand();
// 创建参数
IDataParameter[] parameters = {
new SqlParameter("@param1", SqlDbType.VarChar,20) ,
new SqlParameter("@param2", SqlDbType.Int,15) ,
};
// 设置参数类型
parameters[0].Value = param1; // 设置为输出参数
parameters[1].Value = param2;
// 添加参数
cmd.Parameters.Add(parameters[0]);
cmd.Parameters.Add(parameters[1]);
// 设置sql连接
cmd.Connection = sqlconn;
// 如果执行语句
cmd.CommandText = "MES_TestProcedure";
// 指定执行语句为存储过程
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter dp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
// 填充dataset
dp.Fill(ds);
// 以下是显示效果
for (int t = 0; t < ds.Tables.Count; t++)
{
for (int i = 0; i < ds.Tables[t].Rows.Count; i++)
{
ListViewItem lvi = new ListViewItem();
for (int j = 0; j < ds.Tables[t].Columns.Count; j++)
{
if (j == 0)
lvi.Text = ds.Tables[t].Rows[i][j].ToString();
else
lvi.SubItems.Add(ds.Tables[t].Rows[i][j].ToString());
}
f1.listView1.Items.Add(lvi);
}
}
sqlconn.Close();
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace SqlProcedureCall
{
delegate void InvokeExec();
public partial class Form1 : Form
{
public static string conn = "server=tripodcn-test;uid=sa;pwd=123456;database=R2_sql1";
public Thread[] T = new Thread[10];
public GetSQLResponseRoute[] Route = new GetSQLResponseRoute[10];
public Form1()
{
InitializeComponent();
Form.CheckForIllegalCrossThreadCalls = false;
}
private void button1_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
for (int i = 0; i <10; i++)
{
Route[i] = new GetSQLResponseRoute(this, "test", i);
T[i] = new Thread(new ThreadStart(Route[i].GetSQLResponse));
T[i].Start();
}
}
public class GetSQLResponseRoute {
Form1 f1;
string param1;
int param2;
public GetSQLResponseRoute(Form1 f1, string param1, int param2)
{
this.f1 = f1;
this.param1 = param1;
this.param2 = param2;
}
public void GetSQLResponse() {
SqlConnection sqlconn = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand();
// 创建参数
IDataParameter[] parameters = {
new SqlParameter("@param1", SqlDbType.VarChar,20) ,
new SqlParameter("@param2", SqlDbType.Int,15) ,
};
// 设置参数类型
parameters[0].Value = param1; // 设置为输出参数
parameters[1].Value = param2;
// 添加参数
cmd.Parameters.Add(parameters[0]);
cmd.Parameters.Add(parameters[1]);
// 设置sql连接
cmd.Connection = sqlconn;
// 如果执行语句
cmd.CommandText = "MES_TestProcedure";
// 指定执行语句为存储过程
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter dp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
// 填充dataset
dp.Fill(ds);
// 以下是显示效果
for (int t = 0; t < ds.Tables.Count; t++)
{
for (int i = 0; i < ds.Tables[t].Rows.Count; i++)
{
ListViewItem lvi = new ListViewItem();
for (int j = 0; j < ds.Tables[t].Columns.Count; j++)
{
if (j == 0)
lvi.Text = ds.Tables[t].Rows[i][j].ToString();
else
lvi.SubItems.Add(ds.Tables[t].Rows[i][j].ToString());
}
f1.listView1.Items.Add(lvi);
}
}
sqlconn.Close();
}
}
}
}