Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Smarter Dal

Author  Topic 

OBINNA_EKE
Posting Yak Master

234 Posts

Posted - 2007-01-25 : 07:26:29
Is it possible to automate my insert such that I don't have to repeat

myCommand.Parameters.Add(new SqlParameter("@AssignedStatus", SqlDbType.VarChar, 50));
myCommand.Parameters["@AssignedStatus"].Value = myAssignedStatus.AssignedStatusx;

every time. Imagine if I have 30 fields to insert?
I think there is a way but I don't know how
Can some one help

Thanks

public static bool InsertAssignedStatus(AssignedStatus myAssignedStatus)
{
SqlCommand myCommand = new SqlCommand("InsertAssignedStatus", Conn);
myCommand.CommandType = CommandType.StoredProcedure;

SqlParameter parameterAssignedStatusID = new SqlParameter("@AssignedStatusID", SqlDbType.Int, 4);
parameterAssignedStatusID.Direction = ParameterDirection.Output;
myCommand.Parameters.Add(parameterAssignedStatusID);

myCommand.Parameters.Add(new SqlParameter("@AssignedStatus", SqlDbType.VarChar, 50));
myCommand.Parameters["@AssignedStatus"].Value = myAssignedStatus.AssignedStatusx;

try
{
if (Conn.State == ConnectionState.Closed)
{
Conn.Open();
}
myCommand.ExecuteNonQuery();
}
catch (SqlException e)
{
Console.WriteLine(e.Number);
CallManager.Error.CreateErrorFile(e.Message);
}
finally
{
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
}
if ((int)parameterAssignedStatusID.Value == 0)
{
return false;
}
else
{
return true;
}
}

If it is that easy, everybody will be doing it

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-01-25 : 11:58:56
to save some typing, you can just write:

myCommand.Parameters.Add("@AssignedStatus", SqlDbType.VarChar).Value = xx

as for generating code, there are code generators out there that you can use templates with that will help you generate statements such as these. I know codesmith is one, there's one called MyGeneration, and I've played around with a very simple one call iCodeGenerator .

- Jeff
Go to Top of Page
   

- Advertisement -