public string Test(string userId)
{
string sqlSelect = "Select [dbo].[Test](@user_id)";
SqlParameter[] parms = {
new SqlParameter("@user_id", SqlDbType.VarChar)};
parms[0].Value = userId;
try
{
object returnValue = null;
returnValue = SQLHelper.ExecuteScalar(SQLHelper.ConnectionStringLocalTransaction, CommandType.Text, sqlSelect, parms);
return Convert.ToString(returnValue);
}
catch (Exception ex)
{
throw new BaseException(ex.Message, ex);
}
finally
{
// conn.Close();
}
}
public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection connection = new SqlConnection(connectionString))
{
//try
//{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
connection.Close();
return val;
//}
//finally
//{
// connection.Close();
//}
}
}
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
{
if (parm.SqlValue == null) { parm.SqlValue = DBNull.Value; }
cmd.Parameters.Add(parm);
}
}
}
{
string sqlSelect = "Select [dbo].[Test](@user_id)";
SqlParameter[] parms = {
new SqlParameter("@user_id", SqlDbType.VarChar)};
parms[0].Value = userId;
try
{
object returnValue = null;
returnValue = SQLHelper.ExecuteScalar(SQLHelper.ConnectionStringLocalTransaction, CommandType.Text, sqlSelect, parms);
return Convert.ToString(returnValue);
}
catch (Exception ex)
{
throw new BaseException(ex.Message, ex);
}
finally
{
// conn.Close();
}
}
public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection connection = new SqlConnection(connectionString))
{
//try
//{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
connection.Close();
return val;
//}
//finally
//{
// connection.Close();
//}
}
}
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
{
if (parm.SqlValue == null) { parm.SqlValue = DBNull.Value; }
cmd.Parameters.Add(parm);
}
}
}
No comments:
Post a Comment