Logo 
Search:

Asp.net Answers

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds
  Question Asked By: Clifford Chapman   on Aug 27 In Asp.net Category.

  
Question Answered By: Juan Reynolds   on Aug 27

there's bits missing from that, make a global var private
SqlConnection con; to the class


private void Open()
{
con = new SqlConnection("server=" + server + ";database=" + db + ";User Id=" +
user + ";password=" + pass);
//..........different ways to do this ia web.config, global.asax etc
}

private SqlCommand CreateCommand(string procName, SqlParameter[] prams)
{
SqlCommand cmd = null;
// make sure connection  is open
Open();

cmd = new SqlCommand(procName, con);
cmd.CommandType = CommandType.StoredProcedure;

// add proc parameters
if (prams != null)
{
foreach (SqlParameter parameter in prams)
cmd.Parameters.Add(parameter);
}

// return  param
cmd.Parameters.Add(
new SqlParameter("ReturnValue", SqlDbType.Int, 4,
ParameterDirection.ReturnValue, false, 0, 0,
string.Empty, DataRowVersion.Default, null));

return cmd;
}

/// <summary>
/// close  the connection.
/// </summary>
public void Close()
{
if (con != null)
con.Close();
}

/// <summary>
/// Release resources.
/// </summary>
public void Dispose()
{
// make sure connection is closed
if (con != null)
{
con.Dispose();
con = null;
}
}

//Note execute nonquery requires you to close()

public int RunProc(string procName)
{
SqlCommand cmd = CreateCommand(procName, null);
cmd.ExecuteNonQuery();
this.Close();
return (int)cmd.Parameters["ReturnValue"].Value;
}


Its a bit of a mess and probs not worth it - but its all there now if its any
good to anyone. It's all from PetShop app anyway (altered a bit)
Probs shouldn't have sent it.

Share: 

 

This Question has 3 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on dataset vs. datareader Or get search suggestion and latest updates.


Tagged: