📄 storedprocedure.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace StoredProcedureApp
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Data.SqlClient.SqlConnection sqlConnection1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.listView1 = new System.Windows.Forms.ListView();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.SuspendLayout();
//
// listView1
//
this.listView1.GridLines = true;
this.listView1.Location = new System.Drawing.Point(48, 48);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(264, 120);
this.listView1.TabIndex = 0;
this.listView1.View = System.Windows.Forms.View.Details;
//
// label1
//
this.label1.Location = new System.Drawing.Point(48, 24);
this.label1.Name = "label1";
this.label1.TabIndex = 1;
this.label1.Text = "Result";
//
// button1
//
this.button1.Location = new System.Drawing.Point(48, 200);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(128, 32);
this.button1.TabIndex = 2;
this.button1.Text = "Input=EmployeeID";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(200, 208);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 3;
this.textBox1.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(352, 278);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox1,
this.button1,
this.label1,
this.listView1});
this.Name = "Form1";
this.Text = "StoredProcedure";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
try
{
//clear the list view
listView1.Columns.Clear();
listView1.Items.Clear();
//Create a connection
SqlConnection cn = new SqlConnection(@"Data Source=(local);" +
"Integrated Security=SSPI;database=NorthWind");
//Create a command object
SqlCommand cmd= cn.CreateCommand();
//Specify the stored procedure that is to be executed
cmd.CommandType= CommandType.StoredProcedure;
cmd.CommandText="Orders_ByEmployeeId";
//Create a parameter object to provide the input
SqlParameter parInput = cmd.Parameters.Add("@EmployeeId", SqlDbType.Int);
parInput.Direction = ParameterDirection.Input;
parInput.Value= Convert.ToInt32(textBox1.Text);
//Open the connection
cn.Open();
//execute the command and display the results
//Create a datareader object
SqlDataReader dr=cmd.ExecuteReader();
//Get the colummn names of the datareader and show in the listview's columns
for (int i=0; i< dr.FieldCount; i++)
{
ColumnHeader c = new ColumnHeader();
c.Text=dr.GetName(i);
listView1.Columns.Add(c);
}
//Get the values of each row in the datareader and show them in the list view
ListViewItem itmX;
while (dr.Read())
{
//create the list view item
itmX=new ListViewItem();
//specify the text and subitems of the list view
itmX.Text= dr.GetValue(0).ToString();
for (int i=1 ; i< dr.FieldCount; i++)
{
itmX.SubItems.Add(dr.GetValue(i).ToString());
}
//Add the item to the list view's Items collection
listView1.Items.Add(itmX);
}
//close the reader
dr.Close();
cn.Close();
}
catch ( System.Data.SqlClient.SqlException e1)
{
// Display the error message.
MessageBox.Show ("There was an error in executing the SQL Command. Error Message:" + e1.Message);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -