Click here to Skip to main content
15,882,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
My question is pretty simple but I dont know whats wrong with me
couldnt get my brain working today!!
Okay I created a stored procedure in my sql.
I have three datatables with one coloumn values each which i retrieved from my sqldb.
Now using the store proc I need to update a row in my database table.
I called my store proc but I am getting confusion at the following stage in my code.
Kindly help me out
SqlCommand Cmd3 = new SqlCommand("abc", connection);
Cmd3.CommandType = CommandType.StoredProcedure;
SqlParameter xyz = Cmd3.Parameters.Add("@xyz",dr0["xyz"]);

My question is
1)How do I insert the coloumn value xyz in the table which I need to be updated .
I stored the xyz values in datatable dt and dr0 is the datarow in datable here.
I just need help with the syntax.
Thanks
Lia
Posted

1 solution

This is an example of a cheap homemade data access layer however I'd strongly suggest you look into a third party tool such as: NHibernate, NHydrate or Entity Framework, etc.

You'll need to add input parameters in your stored procedure and then pass those in via the application like so:


C#
// If you're not using a QueryReader then 
// you can create an 'AppDataAccess' singleton 
class SomeDataAccess : AppDataAccess
{

    public int SomeDataAccessMethod(int index, string value)
    {
        return ExecuteNonQuery("procNameHere",
                new SqlParameter("@index", index),
                new SqlParameter("@value", value));
    }
}
// This could be a data access helper. 
internal class AppDataAccess
{
        public string ConnectionString
        {
            get
            {
                return "Your full connection string here";
// OR If grab it from the web.config (or other config file)
// return ConfigurationManager.ConnectionStrings["dbconnstringname"].ConnectionString;
            }
        }



    public int ExecuteNonQuery(string storedProcedure, params SqlParameter[] sqlParameters)
    {
        SqlConnection connection = new SqlConnection(ConnectionString);
        SqlCommand command = new SqlCommand(storedProcedure, connection);
        command.CommandType = CommandType.StoredProcedure;

        if (sqlParameters != null)
        {
            foreach (SqlParameter parameter in sqlParameters)
            {
                command.Parameters.Add(parameter);
            }
        }

        int rows = -1;

        try
        {
            connection.Open();
            rows = command.ExecuteNonQuery();
            connection.Close();
        }
        catch (SqlException ex)
        {
            // Log Exception
        }
        finally
        {
            connection.Dispose();
            command.Dispose();
        }

        return rows;
    }
}


Hope this helps,

Chad
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900