public partial class StoredProcedures
{
[Microsoft.SqlServer.Server.SqlProcedure]
public static int StoredProcedure1(string param)
{
SqlConnection sqlConnection = new SqlConnection("context connection=true");
sqlConnection.Open();
SqlCommand sqlCommand = new SqlCommand("insert into t (name) values ('"+param.Replace("'","''") +"')", sqlConnection);
SqlContext.Pipe.ExecuteAndSend(sqlCommand);
return 0;
}
};
执行该存储过程
private void button1_Click(object sender, EventArgs e)
{
SqlConnection Conn = new SqlConnection("Data Source=202.136.XX.XX;Initial Catalog=testDB;User ID=SA;Password=XXX");
Conn.Open();
SqlCommand sqlcmd = new SqlCommand("StoredProcedure1", Conn);
sqlcmd.CommandType = CommandType.StoredProcedure;
SqlParameter parameter = sqlcmd.Parameters.Add("@param", SqlDbType.NVarChar, 10);
parameter.Value = "SINA";
SqlParameter ret = sqlcmd.Parameters.Add("@Return", SqlDbType.Int);
ret.Direction = ParameterDirection.ReturnValue;
int i = sqlcmd.ExecuteNonQuery();
//存储过程的返回值
MessageBox.Show(ret.Value.ToString());
}