Fetching VB Variables to insert with SQL Query [closed] - sql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Hello everybody hope you are well.
I have a place on a project im working on that im struggling to get past. I'm basically gathering meta data of a song and assigning them to variables in VB.
I'm next trying to save this data to a SQL database with tables i have created in visual studio and im having trouble doing this. The problem lies within trying to link the VB variables into a SQL query. Any Ideas?

To link VB variables to an SQL query, you need to add parameters that store the variables and then use those parameters in your SQL command:
Using parameters
MyCommand = New SqlCommand("SELECT Height, Weight, DoB, Gender, FROM `User` WHERE Username = #Username", DatabaseConnection)
MyCommand.Parameters.AddWithValue("#Username", Username)
UPDATE: How to connect to database
'Leads to the database
Public Filename As String = $"{AppDomain.CurrentDomain.BaseDirectory}YourDatabase.accdb"
'The connection string is set
Public ConStr As String = $"PROVIDER=Microsoft.ACE.OLEDB.12.0;DATA Source = {Filename}"
'The connection to the database is defined
Public DatabaseConnection As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConStr)

Related

JET ODBC SQL in excel, Case statements vs iif [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am getting unrecognized keyword when when I am running a query to join two of my sheets in excel.
Error - 2147467259 - Unrecognized keyword WHEN
This error is due to case statements, they are not accepted in JET ODBC.
I am using JET ODBC and querying within excel sheets.
Can anyone help write a proper case statement using JET ODBC syntax, i can't get this to work.
iif ([stack$].[business_name] = 'GELP'
AND [overflow$].[level] = 'Package' THEN [overflow$].[identifier1]) END AS standardized_identifier,
CASE statements are not supported in SQL when hitting Excel sheets. You are using the JET ODBC/OLEDB provider in Windows which has limited syntax.
Instead use the IIF() function which is similar syntax to Excel's If().
As an example, your first CASE would look like:
iif([stack$].[managementl6description] = 'GLOBAL EQUITY-LINKED PRODUCTS', 'GELP' , iif([stack$].[managementl6description] = 'EQUITY MARKETS', 'EQUITY MARKETS', iif([stack$].[managementl6description] = 'FOREIGN EXCHANGE', 'FIC Foreign Exchange', NULL))) AS business_name

Why some rows can be inserted into and some cannot? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
When I use the Cashier table or some other tables that have small amounts of records the process proceeds and table is inserted into the external database. But when I change the cashier into the transaction database (400k+ records), Visual Studio reports an error near "Transaction" Help would be appreciated thanks.
Cashier Database (working)
Dim query As String = "select * into MyDatabase2.dbo.Cashier from bos_primary_db.dbo.Cashier"
Transaction Database (not working)
Dim query As String = "select * into MyDatabase2.dbo.Transaction from bos_primary_db.dbo.Transaction"
This is the error message:
Incorrect syntax near the keyword 'Transaction'
this is probably because Transaction is a reserved word in SQL.
Depending on your RDBMS (that you didn't specify), there are ways to "escape" it:
for Sql Server, you should wrap reserved words in square brackets:
select * into MyDatabase2.dbo.[Transaction] from bos_primary_db.dbo.[Transaction]
For MySql you should use an apostrophe:
select * into MyDatabase2.dbo.`Transaction` from bos_primary_db.dbo.`Transaction`
For Oracle you should use double quotes:
select * into MyDatabase2.dbo."Transaction" from bos_primary_db.dbo."Transaction"
Note: You should always try to avoid using reserved words. This link describes my favorite way of do it.

Kill Oracle sessions using vb.net [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
How do you get a vb.net program to send a session kill command to Oracle? I can do it in sqlplusw by just typing:
alter system kill session '45,30665';
In vb.net, I'm trying to do this with an oracle.dataAccess object:
oraConn As New OracleConnection("Data Source=db1;User ID=usr;Password=pw")
oraConn.Open()
Dim cmd As New OracleCommand
cmd.Connection = oraConn
cmd.CommandType = CommandType.Text
cmd.CommandText = "alter system kill session '45,30665';"
cmd.ExecuteNonQuery()
cmd.Dispose()
When it gets to the ExecuteNonQuery line, it throws an exception "ORA-00911: invalid character". I think it's expecting an SQL statement. I'm using VB Express 2008.
You don't want semicolons in statements that you're sending from a client. Get rid of the semicolon at the end of the alter system
cmd.CommandText = "alter system kill session '45,30665'"

Getting error The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. The statement has been terminated [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
this is my code
sqlcommand=new sqlcommand("inset into table values('"+home.name'"+,'"+home.DOJ'")",con);
You're using the local default string conversion from DateTime (I assume) to string, and then hoping that's valid for SQL.
Additionally, your code has a SQL injection attack vulnerability.
Both of these can be fixed - and your code readability - can be improved using parameterized SQL:
using (var command = new SqlCommand(
"INSERT INTO Foo (Name, DOJ) Values (#Name, #DOJ)", connection))
{
command.Parameters.Add("#Name", SqlDbType.VarChar).Value = home.name;
command.Parameters.Add("#DOJ", SqlDbType.DateTime).Value = home.DOJ;
...
}
(Adjust the types accordingly, e.g. using SqlDbType.NVarChar and SqlDbType.DateTime2.)
Never put values directly into SQL like you were doing. Using parameterized SQL is a win all round.

Error message from simple SELECT statement with practice database [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I use the AdventureWorks practice database attached to SQL Server 2012.
A simple statement like:
SELECT * FROM HumanResources.Shift;
gives me this error:
Msg 208, Level 16, State 1, Line 1
Invalid object name 'AdventureWorks2012'.
I also tried:
USE master
GO
SELECT * FROM HumanResources.Shift;
Received same error message. Any idea to why this is happening?
Is AdventureWorks2012 the name of a database or a table?
USE <database name>
means use the specified database to run the query against.
SELECT * FROM <table name>
means select all the rows from the specified table.
--Edit because of change in question--
You don't want to use Master, you want to use the name of your DB. Next, using the object explorer, I would make sure the table actually exists in the Database you created.