SqlDataAdapter, Stored Procedure, how to add parameter?

D

dawson

Hello, from the code below, how do I add/send a parameter to the stored
procedure?

SqlConnection conn = null;
conn = new
SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
SqlDataAdapter daProjects = new SqlDataAdapter("dbo.getProjectFiles",
conn);

DataSet ds = new DataSet();
daProjects.Fill(ds);
 
D

DeveloperX

I think something like this:

d.SelectCommand.Parameters.Add(new
System.Data.SqlClient.SqlParameter("paramname","param"));

There are 7 overrides for the constructor on SqlParameter, I've just
picked the simple one. You may also have to add a SqlCommand to the
SelectCommand first.
 
D

dawson

I know how to do it this way ... which works ...

conn = new
SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();

// 1. create a command object identifying
// the stored procedure
SqlCommand cmd = new SqlCommand(
"dbo.insertProjectFile", conn);

// 2. set the command object so it knows
// to execute a stored procedure
cmd.CommandType = CommandType.StoredProcedure;

// 3. add parameter to command, which
// will be passed to the stored procedure
cmd.Parameters.Add(new SqlParameter("@StrFileName", StrFileName));

cmd.ExecuteNonQuery();


But I can't figure out how to do it with the sqladapator as it doesn't
have the cmd.ExecuteNonQuery(); at the end, but just binds to the
dataset with fill!

I think something like this:

d.SelectCommand.Parameters.Add(new
System.Data.SqlClient.SqlParameter("paramname","param"));

There are 7 overrides for the constructor on SqlParameter, I've just
picked the simple one. You may also have to add a SqlCommand to the
SelectCommand first.


Hello, from the code below, how do I add/send a parameter to the stored
procedure?

SqlConnection conn = null;
conn = new
SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
SqlDataAdapter daProjects = new SqlDataAdapter("dbo.getProjectFiles",
conn);

DataSet ds = new DataSet();
daProjects.Fill(ds);
 
D

dawson

http://www.c-sharpcorner.com/UploadFile/dclark/UseSPwithDP11282005035417AM/UseSPwithDP.aspx
I know how to do it this way ... which works ...

conn = new
SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();

// 1. create a command object identifying
// the stored procedure
SqlCommand cmd = new SqlCommand(
"dbo.insertProjectFile", conn);

// 2. set the command object so it knows
// to execute a stored procedure
cmd.CommandType = CommandType.StoredProcedure;

// 3. add parameter to command, which
// will be passed to the stored procedure
cmd.Parameters.Add(new SqlParameter("@StrFileName", StrFileName));

cmd.ExecuteNonQuery();


But I can't figure out how to do it with the sqladapator as it doesn't
have the cmd.ExecuteNonQuery(); at the end, but just binds to the
dataset with fill!

I think something like this:

d.SelectCommand.Parameters.Add(new
System.Data.SqlClient.SqlParameter("paramname","param"));

There are 7 overrides for the constructor on SqlParameter, I've just
picked the simple one. You may also have to add a SqlCommand to the
SelectCommand first.


Hello, from the code below, how do I add/send a parameter to the stored
procedure?

SqlConnection conn = null;
conn = new
SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
SqlDataAdapter daProjects = new SqlDataAdapter("dbo.getProjectFiles",
conn);

DataSet ds = new DataSet();
daProjects.Fill(ds);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top