using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace StoreTest{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btn_StoreInvoke_Click(object sender, EventArgs e) { string conn = "server=.;database=TESTSD;uid=sa;pwd=j6394126"; SqlConnection SqlConnection = new SqlConnection(conn); SqlCommand cmd = new SqlCommand(); cmd.Connection = SqlConnection;//設定sql串連 cmd.CommandText = "getAllData";//設定調用預存程序的名字 cmd.CommandType = CommandType.StoredProcedure;//指定執行語句為預存程序 SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds);//填充dataset this.dgv_StoreView.DataSource = ds.Tables[0]; //dgv_StoreView.DataBind(); 這個是asp.net才需要添加這句,winform設定資料來源後就行了,不用再進行綁定 } private void btn_StoreNoInOutParam_Click(object sender, EventArgs e) { string conn = "server=.;database=TESTSD;uid=sa;pwd=j6394126"; SqlConnection SqlConn = new SqlConnection(conn); SqlCommand cmd = new SqlCommand(); cmd.Connection = SqlConn; cmd.CommandText = "InsertInfo"; cmd.CommandType = CommandType.StoredProcedure; SqlConn.Open(); this.txt_StoreNoInOut.Text = "執行預存程序影響了:"+cmd.ExecuteNonQuery().ToString()+"行"; SqlConn.Close(); } private void btn_Test3_Click(object sender, EventArgs e) { string conn = "server=.;database=TESTSD;uid=sa;pwd=j6394126"; SqlConnection SqlConn = new SqlConnection(conn); SqlCommand cmd = new SqlCommand(); cmd.Connection = SqlConn; cmd.CommandText = "InOutTest"; cmd.CommandType = CommandType.StoredProcedure;//設定使用預存程序 cmd.Parameters.Add("@studentId", SqlDbType.VarChar);//設定預設的輸入參數值 cmd.Parameters.Add("@studentName", SqlDbType.VarChar, 50);//設定預設的輸出參數,否則預設的Size為0 cmd.Parameters.Add("@studentClass", SqlDbType.VarChar, 50);//設定預設的輸出參數,否則預設的Size為0 cmd.Parameters.Add("@studentScores", SqlDbType.Int);//設定預設的輸出參數,否則預設的Size為0 cmd.Parameters.Add("@rowCount", SqlDbType.Int);//設定預設的輸出參數,否則預設的Size為0 cmd.Parameters["@studentId"].Value = 2;//為預設參數賦值 cmd.Parameters["@studentName"].Direction = ParameterDirection.Output;//設定參數為輸出參數,否則預設情況為輸入參數 cmd.Parameters["@studentClass"].Direction = ParameterDirection.Output;//設定參數為輸出參數,否則預設情況為輸入參數 cmd.Parameters["@studentScores"].Direction = ParameterDirection.Output;//設定參數為輸出參數,否則預設情況為輸入參數 cmd.Parameters["@rowCount"].Direction = ParameterDirection.ReturnValue;//設定為傳回值操作 //以下注釋掉的也是一種方式,任選一種 //IDataParameter[] parameters ={ // new SqlParameter("@studentId",SqlDbType.VarChar), // new SqlParameter("@studentName",SqlDbType.VarChar,50), // new SqlParameter("@studentClass", SqlDbType.VarChar, 50), // new SqlParameter("@studentScores",SqlDbType.Int), // new SqlParameter("@rowCount", SqlDbType.Int) // }; //cmd.Parameters.Add(parameters[0]); //cmd.Parameters.Add(parameters[1]); //cmd.Parameters.Add(parameters[2]); //cmd.Parameters.Add(parameters[3]); //cmd.Parameters.Add(parameters[4]); //parameters[0].Value = 2; //parameters[1].Direction = ParameterDirection.Output;//設定參數為輸出參數,否則預設情況為輸入參數 //parameters[2].Direction = ParameterDirection.Output;//設定參數為輸出參數,否則預設情況為輸入參數 //parameters[3].Direction = ParameterDirection.Output;//設定參數為輸出參數,否則預設情況為輸入參數 //parameters[4].Direction = ParameterDirection.ReturnValue;//設定為傳回值操作 SqlConn.Open(); cmd.ExecuteNonQuery(); this.txt_name.Text = cmd.Parameters["@studentName"].Value.ToString();//擷取Id為3的名字 this.txt_Class.Text = cmd.Parameters["@studentClass"].Value.ToString();//擷取Id為3的班級 this.txt_Socres.Text = cmd.Parameters["@studentScores"].Value.ToString() + "-" + "影響了:" + cmd.Parameters["@rowCount"].Value.ToString() + "行";//擷取Id為3的分數 //this.txt_name.Text = parameters[1].Value.ToString();//擷取Id為3的名字 //this.txt_Class.Text = parameters[2].Value.ToString();//擷取Id為3的班級 //this.txt_Socres.Text = parameters[3].Value.ToString() + "-" + "影響了:" + // parameters[4].Value.ToString() + "行";//擷取Id為3的分數 SqlConn.Close(); } }}