存储过程
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 存储过程
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Button b = new Button();
b.Name = "button2";
b.Text = "newButton";
b.Click += new EventHandler(btn_Click);
this.Controls.Add(b);
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=wubin;User ID=sa;Password=123");
con.Open();
string sql = "select * from tyg";
SqlCommand cmd = new SqlCommand(sql);
cmd.Connection = con;
SqlDataReader read = cmd.ExecuteReader();
for (int i = 0; i < read .FieldCount ; i++)
{
this.listView1.Columns.Add (read .GetName (i).ToString ());
}
while (read.Read ())
{
ListViewItem itm = this.listView1.Items.Add(read[0].ToString());
itm.SubItems.Add(read[1].ToString());
itm.SubItems.Add(read[2].ToString());
itm.SubItems.Add(read[3].ToString());
itm.SubItems.Add(read[4].ToString());
}
}
private void btn_Click(object sender, System.EventArgs e)
{
string str = "Data Source=.;Initial Catalog=wubin;User ID=sa;Password=123";
SqlConnection con = new SqlConnection(str);
con.Open();
if (con.State==ConnectionState .Open )
{
MessageBox.Show("成功");
}
else
{
MessageBox.Show("失败");
}
}
private void button2_Click(object sender, EventArgs e)
{
string str = "Data Source=.;Initial Catalog=wubin;User ID=sa;Password=123";
SqlConnection conn = new SqlConnection(str);
conn.Open();
SqlCommand cmd = new SqlCommand("sel", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sel";
cmd.Parameters.Add("@a", SqlDbType.Int).Value = 1;
cmd.Parameters.Add("@c", SqlDbType.VarChar,16) ;
cmd.Parameters[1].Direction = ParameterDirection.Output;
SqlDataReader read = cmd.ExecuteReader();
//while (read .Read ())
//{
// MessageBox.Show(cmd.Parameters [0].Value .ToString ());
//}
MessageBox.Show(cmd.Parameters [0].Value .ToString ()+" "+cmd.Parameters[1].Value .ToString ());
}
}
}