C#中使用临时存储过程
Code
string strSql = "";
SqlConnection sqlCon = new SqlConnection(HeadStartConnString);
SqlCommand sqlCommStoreProcedue = new SqlCommand();
sqlCommStoreProcedue.Connection = sqlCon;
sqlCommStoreProcedue.CommandType = CommandType.Text;
if (chk.Checked)
{
strSql = "Create PROCEDURE #ImgeUpdate \n @RptImage Image, \n @RptDescription varchar(1000) \n as update Reports set RptImage = @RptImage, RptDescription = @RptDescription where RptID = " + rptID;
}
else
strSql = "Create PROCEDURE #ImgeUpdate \n @RptDescription varchar(1000) \n as update Reports set RptDescription = @RptDescription where RptID = " + rptID;
sqlCommStoreProcedue.CommandText = strSql;
sqlCon.Open();
sqlCommStoreProcedue.ExecuteNonQuery();
SqlCommand sqlCom = new SqlCommand();
sqlCom.Connection = sqlCon;
sqlCom.CommandType = CommandType.StoredProcedure;
sqlCom.CommandText = "#ImgeUpdate";
if (chk.Checked)
{
if (filUL.HasFile)
{
int intImgSize = filUL.PostedFile.ContentLength;
Stream ImgStream = filUL.PostedFile.InputStream;
byte[] ImgContent = new byte[intImgSize];
int intStatus = ImgStream.Read(ImgContent, 0, intImgSize);
SqlParameter prmImg = new SqlParameter("@RptImage", SqlDbType.Image);
prmImg.Value = ImgContent;
sqlCom.Parameters.Add(prmImg);
sqlCom.Parameters["@RptImage"].Direction = ParameterDirection.Input;
}
}
SqlParameter prmDes = new SqlParameter("@RptDescription", SqlDbType.VarChar, 1000);
prmDes.Value = txtDesp.Text.ToString().Trim();
sqlCom.Parameters.Add(prmDes);
sqlCom.Parameters["@RptDescription"].Direction = ParameterDirection.Input;
try
{
sqlCom.ExecuteNonQuery();
ClientScript.RegisterStartupScript(this.GetType(), "closeWin_Save", "<script>alert('Save This Record Succeed!');window.close();</script>");
}
catch (SqlException sqlE)
{
ClientScript.RegisterStartupScript(this.GetType(), "Error_Msg", "<script>alert('Save This Record Failing:" + sqlE.Message + "');</script>");
}
finally
{
sqlCommStoreProcedue.Dispose();
sqlCom.Dispose();
sqlCon.Close();
}
string strSql = "";
SqlConnection sqlCon = new SqlConnection(HeadStartConnString);
SqlCommand sqlCommStoreProcedue = new SqlCommand();
sqlCommStoreProcedue.Connection = sqlCon;
sqlCommStoreProcedue.CommandType = CommandType.Text;
if (chk.Checked)
{
strSql = "Create PROCEDURE #ImgeUpdate \n @RptImage Image, \n @RptDescription varchar(1000) \n as update Reports set RptImage = @RptImage, RptDescription = @RptDescription where RptID = " + rptID;
}
else
strSql = "Create PROCEDURE #ImgeUpdate \n @RptDescription varchar(1000) \n as update Reports set RptDescription = @RptDescription where RptID = " + rptID;
sqlCommStoreProcedue.CommandText = strSql;
sqlCon.Open();
sqlCommStoreProcedue.ExecuteNonQuery();
SqlCommand sqlCom = new SqlCommand();
sqlCom.Connection = sqlCon;
sqlCom.CommandType = CommandType.StoredProcedure;
sqlCom.CommandText = "#ImgeUpdate";
if (chk.Checked)
{
if (filUL.HasFile)
{
int intImgSize = filUL.PostedFile.ContentLength;
Stream ImgStream = filUL.PostedFile.InputStream;
byte[] ImgContent = new byte[intImgSize];
int intStatus = ImgStream.Read(ImgContent, 0, intImgSize);
SqlParameter prmImg = new SqlParameter("@RptImage", SqlDbType.Image);
prmImg.Value = ImgContent;
sqlCom.Parameters.Add(prmImg);
sqlCom.Parameters["@RptImage"].Direction = ParameterDirection.Input;
}
}
SqlParameter prmDes = new SqlParameter("@RptDescription", SqlDbType.VarChar, 1000);
prmDes.Value = txtDesp.Text.ToString().Trim();
sqlCom.Parameters.Add(prmDes);
sqlCom.Parameters["@RptDescription"].Direction = ParameterDirection.Input;
try
{
sqlCom.ExecuteNonQuery();
ClientScript.RegisterStartupScript(this.GetType(), "closeWin_Save", "<script>alert('Save This Record Succeed!');window.close();</script>");
}
catch (SqlException sqlE)
{
ClientScript.RegisterStartupScript(this.GetType(), "Error_Msg", "<script>alert('Save This Record Failing:" + sqlE.Message + "');</script>");
}
finally
{
sqlCommStoreProcedue.Dispose();
sqlCom.Dispose();
sqlCon.Close();
}
存储过程的效率是比较高的。
继续追寻。。。。。。