thenrich
asked on
Update DataSource Question
Using ADO.NET how would I make a simple Update to my data source???
Answer in VB please ...
Answer in VB please ...
ASKER
Is there anyway I can do this without the "Select" ??
ASKER
I guess I'm looking for a way to do this with having to go though the process of creating a dataset.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi,
a basic pattern for doing it the quick'n dirty way :
Dim conn as OleDbConnection = new OleDbConnection("your connection string here")
Dim cmd as OleDbCommand = conn.CreateCommand()
cmd.CommandType = CommandType.Text
cmd.CommandText = "update toto set tata = '" & myValue & "'"
cmd.ExecuteNonQuery()
cmd.Dispose()
conn.Close()
-S
a basic pattern for doing it the quick'n dirty way :
Dim conn as OleDbConnection = new OleDbConnection("your connection string here")
Dim cmd as OleDbCommand = conn.CreateCommand()
cmd.CommandType = CommandType.Text
cmd.CommandText = "update toto set tata = '" & myValue & "'"
cmd.ExecuteNonQuery()
cmd.Dispose()
conn.Close()
-S
ASKER
I'm going to have to give the points to naveenkohli cause he was first with the correct response.
ASKER
I'll give you guys 25 more points if you can tell me how to add Parameters, I've got this now:
l_OLECommand = l_OLEConnection.CreateComm and
l_OLECommand.CommandText = UpdateString
l_OLECommand.Parameters.Ad d("@test2" , OleDbType.VarChar, Len("OUT")).Value = "OUT"
l_OLECommand.Parameters.Ad d("@test1" , OleDbType.VarChar, Len("7")).Value = "7"
l_OLECommand.ExecuteNonQue ry()
This works without Parms
l_OLECommand = l_OLEConnection.CreateComm
l_OLECommand.CommandText = UpdateString
l_OLECommand.Parameters.Ad
l_OLECommand.Parameters.Ad
l_OLECommand.ExecuteNonQue
This works without Parms
First mark the command type to be of type StoredProcuedure (if you are dealing with SP)
l_OLECommand.CommandType = CommandType.StoredProcedur e;
Then call ExecuteQuery.
l_OLECommand.CommandType = CommandType.StoredProcedur
Then call ExecuteQuery.
ASKER
SP ??
ASKER
oooooooo SP - Stored Procedure. Nope not dealing with them
ASKER
Let me ask you this - why wouldn't the above code work ??
ASKER
I've got this:
UpdateString = "Update CTO_TEST Set STATUS=? Where ID=? "
l_OLECommand = l_OLEConnection.CreateComm and
l_OLECommand.CommandText = UpdateString
l_OLECommand.Parameters.Ad d("@test2" , OleDbType.VarChar, Len("OUT")).Value = "OUT"
l_OLECommand.Parameters.Ad d("@test1" , OleDbType.VarChar, Len("7")).Value = "7"
l_OLECommand.CommandType = CommandType.Text
l_OLECommand.ExecuteNonQue ry()
This doesn't error out but it does update the DataSource either.
UpdateString = "Update CTO_TEST Set STATUS=? Where ID=? "
l_OLECommand = l_OLEConnection.CreateComm
l_OLECommand.CommandText = UpdateString
l_OLECommand.Parameters.Ad
l_OLECommand.Parameters.Ad
l_OLECommand.CommandType = CommandType.Text
l_OLECommand.ExecuteNonQue
This doesn't error out but it does update the DataSource either.
What is your update string..
ASKER
Dim UpdateString as string
UpdateString = "Update CTO_TEST Set STATUS=? Where ID=? "
UpdateString = "Update CTO_TEST Set STATUS=? Where ID=? "
Try this..
l_OLECommand.Parameters.Ad d("@Status ", OleDbType.VarChar, Len("OUT")).Value = "OUT"
l_OLECommand.Parameters.Ad d("@ID", OleDbType.VarChar, Len("7")).Value = "7"
l_OLECommand.Parameters.Ad
l_OLECommand.Parameters.Ad
ASKER
I tried - No luck. I think that's just a Parm name.
ASKER
is it possible that command parameters can only be used with queries and not nonquery items?
HEre is sample from Documentattion. IT does exactly what you are trying to do.
Dim catDA As OleDbDataAdapter = New OleDbDataAdapter("SELECT CategoryID, CategoryName FROM Categories", nwindConn)
catDA.UpdateCommand = New OleDbCommand("UPDATE Categories SET CategoryName = ? " & _
"WHERE CategoryID = ?" , nwindConn)
catDA.UpdateCommand.Parame ters.Add(" @CategoryN ame", OleDbType.VarChar, 15, "CategoryName")
Dim catDA As OleDbDataAdapter = New OleDbDataAdapter("SELECT CategoryID, CategoryName FROM Categories", nwindConn)
catDA.UpdateCommand = New OleDbCommand("UPDATE Categories SET CategoryName = ? " & _
"WHERE CategoryID = ?" , nwindConn)
catDA.UpdateCommand.Parame
ASKER
The problem is I have 2 SQL's a Select and an Update. I just want an Update.
Dim catDA As SqlDataAdapter = New SqlDataAdapter("SELECT CategoryID, CategoryName FROM Categories", nwindConn)
catDA.UpdateCommand = New SqlCommand("UPDATE Categories SET CategoryName = @CategoryName " & _
"WHERE CategoryID = @CategoryID", nwindConn)
catDA.UpdateCommand.Parame
Dim workParm As SqlParameter = catDA.UpdateCommand.Parame
workParm.SourceColumn = "CategoryID"
workParm.SourceVersion = DataRowVersion.Original
Dim catDS As DataSet = New DataSet
catDA.Fill(catDS, "Categories")
Dim cRow As DataRow = catDS.Tables("Categories")
cRow("CategoryName") = "New Category"
catDA.Update(catDS)