create proc usp_updateDeskInfo
@id int,
@name nvarchar(10),
@result nvarchar(10) output
as
begin
update DeskInfo set DeskName=@name where DeskId=@id
set @result='sd'
end
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 执行存储过程
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
LoadDeskInfo();
}
private void LoadDeskInfo()
{
string str = @"Data Source=admin1610311105\SQLEXPRESS;Initial Catalog=MyDataBaseNew;Integrated Security=True;User Id=sa;Password=123456";
DataTable dt = new DataTable();
string sql = "usp_selectDeskInfo";
using (SqlDataAdapter sda = new SqlDataAdapter(sql, str))
{
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
sda.Fill(dt);
}
dataGridView1.DataSource = dt;
}
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
string str = @"Data Source=admin1610311105\SQLEXPRESS;Initial Catalog=MyDataBaseNew;Integrated Security=True;User Id=sa;Password=123456";
string sql = "usp_updateDeskInfo";
using (SqlConnection con = new SqlConnection(str))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@id", SqlDbType.Int));
cmd.Parameters["@id"].Value = 2;
cmd.Parameters.Add(new SqlParameter("@name", SqlDbType.NVarChar));
cmd.Parameters["@name"].Value = "sk1";
cmd.Parameters.Add(new SqlParameter("@result", SqlDbType.NVarChar, 10));
cmd.Parameters["@result"].Direction = ParameterDirection.Output;
i = cmd.ExecuteNonQuery();
MessageBox.Show(cmd.Parameters["@result"].Value.ToString());
}
}
if (i > 0)
{
MessageBox.Show("成功");
LoadDeskInfo();
}
else
{
MessageBox.Show("失败");
}
}
}
}