By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,312 Members | 1,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,312 IT Pros & Developers. It's quick & easy.

C# SqlParameter SqlDataAdapter

P: n/a
Hi,

I want to use the Sqlparameter and SqlDataAdapter to update my data,
and the data will be updated based on two TextBoxes txtCustName and
txtCustAddress.
Thanks for help.

Jason
Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Could you provide a bit more information ...
the following example show how to update a particular customer's data based on
the value of txtCustName and the value of txtCustAddress

//assume there is a SqlConnection called myConnection
SqlCommand myCommand = new SqlCommand("", myConnection);
myCommand.CommandText = "UPDATE Customers SET CustomerData = @Data WHERE
CustomerName = @CusName AND CustomerAddress = @CusAddress";
myCommand.Parameters.Add(new SqlParameter(@CusName, txtCustName.Text);
myCommand.Parameters.Add(new SqlParameter(@CusAddress,
txtCustAddress.Text);
myCommand.Parameters.Add(new SqlParameter(@CusData, txtCustData.Text);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
}

SqlDataAdapter is only used when u update the database from a dataset

cheers,
Ivan

"Jason Huang" wrote:
Hi,

I want to use the Sqlparameter and SqlDataAdapter to update my data,
and the data will be updated based on two TextBoxes txtCustName and
txtCustAddress.
Thanks for help.

Jason

Nov 17 '05 #2

P: n/a
@Data should be @CusData

"Ivan Wong" wrote:
Could you provide a bit more information ...
the following example show how to update a particular customer's data based on
the value of txtCustName and the value of txtCustAddress

//assume there is a SqlConnection called myConnection
SqlCommand myCommand = new SqlCommand("", myConnection);
myCommand.CommandText = "UPDATE Customers SET CustomerData = @CusData WHERE
CustomerName = @CusName AND CustomerAddress = @CusAddress";
myCommand.Parameters.Add(new SqlParameter(@CusName, txtCustName.Text);
myCommand.Parameters.Add(new SqlParameter(@CusAddress,
txtCustAddress.Text);
myCommand.Parameters.Add(new SqlParameter(@CusData, txtCustData.Text);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
}

SqlDataAdapter is only used when u update the database from a dataset

cheers,
Ivan

"Jason Huang" wrote:
Hi,

I want to use the Sqlparameter and SqlDataAdapter to update my data,
and the data will be updated based on two TextBoxes txtCustName and
txtCustAddress.
Thanks for help.

Jason

Nov 17 '05 #3

P: n/a
Thanks Ivan.
I am thinking in the "myCommand.Parameters.Add(new SqlParameter(@CusName,
txtCustName.Text);", do we need to specify the SqlDbType for the CusName
column?
Will that make any difference?

Jason

"Ivan Wong" <Iv******@discussions.microsoft.com> glsD:8C********************************* *@microsoft.com...
@Data should be @CusData

"Ivan Wong" wrote:
Could you provide a bit more information ...
the following example show how to update a particular customer's data
based on
the value of txtCustName and the value of txtCustAddress

//assume there is a SqlConnection called myConnection
SqlCommand myCommand = new SqlCommand("", myConnection);
myCommand.CommandText = "UPDATE Customers SET CustomerData = @CusData
WHERE
CustomerName = @CusName AND CustomerAddress = @CusAddress";
myCommand.Parameters.Add(new SqlParameter(@CusName, txtCustName.Text);
myCommand.Parameters.Add(new SqlParameter(@CusAddress,
txtCustAddress.Text);
myCommand.Parameters.Add(new SqlParameter(@CusData, txtCustData.Text);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
}

SqlDataAdapter is only used when u update the database from a dataset

cheers,
Ivan

"Jason Huang" wrote:
> Hi,
>
> I want to use the Sqlparameter and SqlDataAdapter to update my data,
> and the data will be updated based on two TextBoxes txtCustName and
> txtCustAddress.
> Thanks for help.
>
> Jason
>
>
>

Nov 17 '05 #4

P: n/a
Absolutely, you should specify the sqldbtype for each of the sqlParameter
in the example the sqlDbType of the @CusName is varChar, if the
CustomerName column has a different type then it will throw exception ...

It is always important to specify the type and the precision of the value
in order to avoid sql injection attack.

Hope it helps
Ivan

"Jason Huang" wrote:
Thanks Ivan.
I am thinking in the "myCommand.Parameters.Add(new SqlParameter(@CusName,
txtCustName.Text);", do we need to specify the SqlDbType for the CusName
column?
Will that make any difference?

Jason

"Ivan Wong" <Iv******@discussions.microsoft.com> ¼¶¼g©ó¶l¥ó·s»D:8C*********************** ***********@microsoft.com...
@Data should be @CusData

"Ivan Wong" wrote:
Could you provide a bit more information ...
the following example show how to update a particular customer's data
based on
the value of txtCustName and the value of txtCustAddress

//assume there is a SqlConnection called myConnection
SqlCommand myCommand = new SqlCommand("", myConnection);
myCommand.CommandText = "UPDATE Customers SET CustomerData = @CusData
WHERE
CustomerName = @CusName AND CustomerAddress = @CusAddress";
myCommand.Parameters.Add(new SqlParameter(@CusName, txtCustName.Text);
myCommand.Parameters.Add(new SqlParameter(@CusAddress,
txtCustAddress.Text);
myCommand.Parameters.Add(new SqlParameter(@CusData, txtCustData.Text);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
}

SqlDataAdapter is only used when u update the database from a dataset

cheers,
Ivan

"Jason Huang" wrote:

> Hi,
>
> I want to use the Sqlparameter and SqlDataAdapter to update my data,
> and the data will be updated based on two TextBoxes txtCustName and
> txtCustAddress.
> Thanks for help.
>
> Jason
>
>
>


Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.