OleDbCommandBuilder OleDbDataAdapter Problem VB.Net - database

I have a field in a 2003 access database named first. I run the data adapter update command, gives me "Syntax error in UPDATE statement." I change the field name to firstName, try the update again, it works. I already have a bunch of programs using the database and using the field "first", do I really have to rename my field, go back and update all my programs to read from firstName instead of first? I really don't want to go that route if I don't have to. Any suggestions? Thanks!
(provider=microsoft.jet.oledb.4.0)
*Edit: I meant to specify, I want the answer to use commandbuilder (but it won't work that way). Sorry for any confusion.

Do this with your commandbuilder object:
Dim cmdBuilder As New OleDb.OleDbCommandBuilder(oledbAdapter)
cmdBuilder.QuotePrefix = "["
cmdBuilder.QuoteSuffix = "]"
This will put brackets around all field names. I had the same problem with a field named Index and this resolved it.

Try putting square brackets around the field name, [first].

oledbcommandbuilder breaks when fields are named with the reserved word "first". Had to write my own SQL

Related

VB.NET Database reading error

I have this SQL sentence for retrieving a specific role (by the column RoleID) from the table Roles:
DBBroker.getInstance.read("SELECT * FROM Roles WHERE RoleID='" & role.roleID & "';")
The thing is that when my program runs the sentence, I obtain this error:
I work with a Microsoft Access Database in which RoleID is defined as Autonumber while in my program it is defined as String. Anyway I tried changing Types but it still fails.
I have too much code in the program so I cannot include it here, but I'm open for any requests regarding a specific part of the program.
Thanks
By the way I worked before on another similar database and the exact same clause did work indeed.
Though you solved your problem,i am answering this just to describe and help guys who visit this page in future
Here's a one line explanation of the issue :
The data type you set for the column in the table of the database is different than the type of value you are passing
For example : In simple words, you have a word and a number , can you add them ? I mean in a mathematical way ? The answer is NO.
Now assuming that your data-type for the RoleID column is Integer but role.RoleId is of type/returns value of type String , then there will be a data-type mismatch as one is an integer and the other is a String.
Now, going through the comments , i see that you've solved your issue.Now, let me explain how you solved it.
Your sql query looks like this :
"SELECT * FROM Roles WHERE RoleID='" & role.roleID & "';"
Let's point out the main relevant part :
RoleID='" & role.roleID & "'
Here,before you close the string RoleID= with double quotes, you use '(single quote).In sql-queries, single quotes are used to declare/give a value(of type STRING) to the required/given parameter(of the query).
In order to pass an Integer value ,you can pass it without the ' single quote like this :
RoleID=12345
So, the answer is simple:You were passing some data of type String to a column which expects data/even if the passed value is of type intger but because you were using ' single quotes when passing the values,the quote had converted it to a String ..So, all u have to do(had to do-in your case) is remove the two single quotes(')
Hope this helps to enrich your knowledge :)

UiPath scrape into sql database

Not sure how many of you have used UiPath, but I am having an issue when scrapping within UiPath and trying to insert that scrapped variable into my database. It keeps telling me it is not the right column name. Any suggestions?
To pass the contents of the variable returned by ScrapeActivity to the query you must first declare a parameter in the "Parameters" property of ExecuteNonQuery activity.
Let's say your ScrapeActivity returns the scraped text in scrapeActivityResult variable.
Click on "Edit Query" and afterwards the "Parameters" button.
A new dialog will open where you can add parameters.
Add a new In parameter named companyName of type "String" and set its value to scrapeActivityResult variable.
After that, you have to use the newly created parameter(companyName) into your query
"INSERT INTO tblCompany('CompanyName') VALUES (#companyName)"
That was happening to me too! There is a parameter in the query activity that needs to be set/not set
It's really good to see uipath stuff here...

SQL Server: Parameter is not replaced

I've got a fully functionable (secure) session to a SQL Server database (version 10.50.4000). This is stored in a public variable:
SqlConnection conn = new SqlConnection();
I only want to run SELECT queries. For anything else, the user account got no rights.
The queries are built with only one user entry, which is inserted into a simple Text Box.
Unfortunately I must not tell you the original command text. So I make it simple for you:
function print_users(string filtervalue)
{
SqlCommand cmd = null;
cmd = new SqlCommand("SELECT users From groups WHERE group_name LIKE '%#fv%'", this.conn)
cmd.Parameters.Add("#fv", SqlDbType.NVarChar);
cmd.Parameters["#fv"].Value=filtervalue;
rdr = cmd.ExecuteReader();
while(rdr.Read())
{
//Do something with the answer from the DB
}
}
But this does not do the trick. I also tried AddWithValue, but I got no luck.
When creating a stop-point on the line, where #fv should be replaced, I can go through the code line-by-line. And I can see that the command, where #fv should be replaced, is processed with no error. But #fv is not replaced (or at least I cannot see the replacement in the debug console).
What am I doing wrong?
EDIT:
thank you for your replies. Leaving out the single quotes ( ' ) did the trick.
And I also learned that this is not a string replacement. Thank you.
Just one word: The connection is not left open all the time. It's immediately closed, when it's not needed any more; and re-established when needed again - I just forgot to write that into my sample code.
Again: Thank you for your help!
You can't see it being replaced in your debug session; the replacement occurs in the SQL server code itself...
The client (your code) send both the SQL-string and the value of the parameter as seperate things to the server. There the SQL Engine 'replaces' the parameter with its value while executing it.
You should also put the 'wildcards' inside your parametervalue, not inside the query.
cmd = new SqlCommand("SELECT users From groups WHERE group_name LIKE #fv ", this.conn)
cmd.Parameters.Add("#fv", SqlDbType.NVarChar);
cmd.Parameters["#fv"].Value= "%" + filtervalue + "%";
The parameter is not working because it is inside a string literal. You want to build the string like this:
cmd = new SqlCommand("SELECT users From groups WHERE group_name LIKE '%' + #fv + '%'");
While we're at it, keeping a global connection like that is bad. It can cause strange side effects, especially in web apps. Instead, keep a global connection string, and then use that string to create a new connection on each request.
Also, "replace" is the wrong word here. Sql parameters are never replaced, even when they work. That's the whole point. There is no string replacement into your query at any point, ever. It's more like you declared an #fv variable at the server level in a stored procedure, and assigned your data directly to that variable. In this way, there is no possibility for a vulnerability in parameter replacement code, because the data portion of your query remains separate throughout the execution process. In same way, don't think in terms of "sanitizing" a parameter for a query; instead, think in terms of quarantining the data.

Database access excel formatting or if statement or grouping

I have a database formatting problem in which I am trying to concatenate column "B" rows based on column "A" rows. Like So:
https://docs.google.com/spreadsheet/ccc?key=0Am8J-Fv99YModE5Va3hLSFdnU0RibmQwNVFNelJCWHc
Sorry I couldn't post a picture. I don't have enough reputation points YET. I'LL Get them eventually though
So I'd like to solve this problem within Excel or Access. Its currently an access database, but I can export it to excel easily. As you can see, I want to find "userid" in column A and where there are multiple column A's such as "shawn" I'd like to combine the multiple instances of shawn and concatenate property num as such.
Even though there are multiple instances of column A still, I could just filter all unique instances of the table later. My concern is how to concatenate column B with a "|" in the middle if column A has multiple instances.
This is just a segment of my data (There is a lot more), so I would be very thankful for your help.
The pseudo code in my head so far is:
If( Column A has more than one instance)
Then Concatenate(Column B with "#"+ "|" +"#")
I'm also wondering if there is a way to do this on access with grouping.
Well Anyways, PLEASE HELP.
In excel we can achieve it easily by custom function in vba module. Hopefully using vba(Macros) is not an issue for you.
Here is the code for the function which can be added in vba. (Press Alt+F11, this will take you to visual editor, right click the project and add a module. Add the below code in module)
Public Function ConcatenatePipe(ByVal lst As Range, ByVal values As Range, ByVal name As Range) As String
ConcatenatePipe = ""
Dim i As Integer
For i = 1 To lst.Count
If name.Value = lst.Item(i).Value Then ConcatenatePipe = ConcatenatePipe & "|" & values.Item(i).Value
Next
ConcatenatePipe = Mid(ConcatenatePipe, 2)
End Function
This function you can use in excel in F Column of your example. Copy the below formulla in F2 and the copy paste the cell to rest of F column. =ConcatenatePipe($A$2:$A$20,$B$2:$B$20,E2)
I believe you can solve this with an SQL GROUP BY function. At least, here's how I'd do it in MySQL or similar:
SELECT userid, GROUP_CONCAT(propertynum SEPARATOR '|') FROM Names GROUP BY userid
as described in this stack overflow post: How to use GROUP BY to concatenate strings in MySQL?
Here's a link on how to use SQL in MS Access: http://www.igetit.net/newsletters/Y03_10/SQLInAccess.aspx
Unfortunately there is not a GROUP_CONCAT function in MSAccess, but this other SO post explains some ways round that: is there a group_concat function in ms-access?

VB.NET - What is wrong with my SQLinsert? Access database

I have a database with a bunch of stuff in it, and right now I'm reading in data, doing some processing on it, and then sticking it in a new database. My code generates this string:
query_string = "INSERT INTO OrgPhrase (EXACT_PHRASE,Org_ID) VALUES (HELLO,123)"
Then it's used this way:
Dim InsertCmd = New System.Data.OleDb.OleDbCommand(query_string, connection)
InsertCmd.ExecuteNonQuery()
The associated database (OLEdb connection) exists and opens fine, with all the tables and columns it's trying to work with already existing. The error message I get is "No value given for one or more required parameters"
Am I missing something? Did I spell something wrong? I don't have a ton of experience with database work, but I've never had this trouble inserting before.
I believe the query should be
query_string = "INSERT INTO OrgPhrase (EXACT_PHRASE,Org_ID) VALUES ('HELLO',123)"
Also, it may happen that the table has more than 2 columns that are NOT NUll and the values to them are required.
Consider parameterizing the query string. There are a couple of reasons for this. First, you can pass in the values without having to worry about whether or not you need single quotes. Second, you prevent SQL injection.
query_string = "INSERT INTO OrgPhrase (EXACT_PHRASE,Org_ID) VALUES (#ExactPhrase,#OrgId)"
You then create parametes based on the parameter names in the string. Unless, of course, your query string is always the same values, but that sounds a bit too hardcoded to be good.

Resources