How to create database and add 2 table in SQLite - vb.net

I am using SQLite ADO.NET Provider.
I want to create a database with 2 tables via code in vb.net .
Please provide code for the same.
I am using VS 2010 Winforms, working under XP SP3 Pro

Use the SQLiteConnection's CreateFile() method.
SQLiteConnection.CreateFile("c:\\mydatabasefile.db3")
More info on the System.Data.SQLite forums
You can then send ad-hoc CREATE TABLE statements to the engine:
dim myTableCreate as String =
"CREATE TABLE MyTable(CustomerID INTEGER PRIMARY KEY ASC,
FirstName VARCHAR(25));"
cmd.CommandText = myTableCreate
cmd.ExecuteNonQuery()
More on SQLite CREATE TABLE.

For those who need this, here is an updated working code...
SQLiteConnection.CreateFile("c:\mydatabasefile.db3")
Using Query As New SQLiteCommand()
Connection.ConnectionString ="DataSource=c:\mydatabasefile.db3;Version=3;New=False;Compress=True;"
Connection.Open()
With Query
.Connection = Connection
.CommandText = "CREATE TABLE MyTable(CustomerID INTEGER PRIMARY KEY ASC, FirstName VARCHAR(25))"
End With
Query.ExecuteNonQuery()
Connection.Close()
End Using

Related

Can I select fetch the primary key of a table through another element that is not unique?

I have a combo box where the user selects a Username that is in a table, I want to know if there is any way I can fetch that table's primary key through that Username.
I tried it, but it just can't seem to get the data, I don't think that it's going to work since a username is not unique.
SELECT * FROM Users WHERE User = John
I just want a way to get the primary key through the username.
If you actually are trying to get a PK from a Query you can do this with ExecuteScalar method. The reason you query is not working is because you need to enclose the search string in single quotes. 'johnny' or user parameters, which is the preferred method. Below is an example that assumes the PK is of type integer.
Jimi raises a good point, if you already have the data in a datatable you can simply just use the pkcolumn as a ValueMember on the combobox and not make another trip to the DB
Private Function GetPK(ByVal Uid As String) As Integer
Using SqlConn As New SqlClient.SqlConnection("Your connection string")
Using Cmd As New SqlClient.SqlCommand("SELECT TOP 1 PKColumn FROM TABLENAME WHERE UsernameColumn =#UserName")
Cmd.Parameters.Add("#UserName", SqlDbType.VarChar).Value = Uid
Dim PkObj As Object = Cmd.ExecuteScalar
If PkObj IsNot Nothing Then
Return CType(PkObj, Integer)
Else
'user not found
Return Nothing
End If
End Using
End Using
End Function

Display Table Records from AS400 to Visual Basic

I'm trying to display the records from PYLEAVE table, but when I use this code, it shows error SQL0204, can someone help me?
Call takeconnectionas400()
conn.Close()
conn.Open()
adapter = New OleDbDataAdapter("select * from PRIMA.PYLEAVE", conn)
ds = New DataSet
adapter.Fill(ds, "PRIMA.PYLEAVE")
DGVAS400.DataSource = ds.Tables("PRIMA.PYLEAVE")
DGVAS400.ReadOnly = True
In DB2 for IBM i, the syntax for the table is not DBNAME.TABLENAME it is SCHEMA.TABLENAME In old AS/400 terminology, that is LIBRARY.FILE Have your IBM i administrator tell you what schema (library) the table is in and change your SELECT statement appropriately.
I don't know VB, but in JDBC you specify the database name in the connection string, not the SELECT statement.

Incorrect SQL syntax error

I have a SQL statement to delete a record from a database table. After stepping through it, I can see that it all works correctly, except for the final part (that is to say, it correctly gets the desired row to delete and all variables match the values that they should be). I'm new to using SQL, so can anybody tell me how to fix the following error at all?
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: Incorrect syntax near '*'.
SQL query:
Public Shared Sub deleteRecord(ByVal recordID As Integer, con As OleDbConnection)
Dim Dc As New OleDbCommand
Dc.Connection = con
con.Open()
Dc.CommandText = "DELETE * FROM tblData WHERE([recordID] = '" & recordID & "')"
Dc.ExecuteNonQuery()
con.Close()
End Sub
Just remove the * and the parentheses. DELETE FROM tblData WHERE recordID = ...
replace with this:
Dc.CommandText = "DELETE FROM tblData WHERE [recordID] = " & recordID
* is used in lieu of typing out all of the column names you want to SELECT. The important thing to note here is that it represents columns.
DELETE occurs against rows. Therefore, saying DELETE * does not make any sense. You can't delete single columns/cells in this way.
Correct syntax is DELETE FROM [TABLE] WHERE [FILTERS]

OracleDataAdapter with GlobalTemporaryTable: FillSchema not working

I'm using FillSchema on a OracleDataAdapter in .net. The SelectCommand works on a global temporary table.
For the first run, this works great. I will get the schema of the global temporary table
Then I drop that temporary table and great a new temporary table with a different schema.
After that, for the second run, the FillSchema method will still return the schema from the old dropped temporary table.
Am I missing something? Shouldn't the select command query the schema from the new version of the temp table?
Thanks for any help!
What did not work:
The OracleDataAdapter.SelectCommand that was used by
FillSchema was:
Select * from TableName
What works:
I needed to change that query to the exact schema,
then it works:
Select column1,column2,column3 from TableName
I don't understand exactly why, but that solved my problem.
It will return the schema of the new global temporary table.
Is this a caching problem of the oracle server? Because the
tableName is always the same?
To use procedure ways to solve this Problem
OracleParameter inputParam = new OracleParameter("TABLE_NAME_IN",OracleDbType.Varchar2,"TEST",ParameterDirection.Input); //Query TableName
OracleParameter refParam = new OracleParameter("OUTPUT",OracleDbType.RefCursor,ParameterDirection.Output);//RefCursor
DataTable dt = new DataTable();//Fill DataTable
using (OracleCommand dbCommand = new OracleCommand("PKG_SYS.SELECT_TABLE_DATA",orclConnection))
{
dbCommand.CommandType = CommandType.StoredProcedure;
dbCommand.Parameters.Add(inputParam);
dbCommand.Parameters.Add(refParam);
using (OracleDataAdapter da = new OracleDataAdapter())
{
da.SelectCommand = dbCommand;
da.Fill(dt);
}
}

SQL Server - Update rows based on a list

I'm working with Windows Forms - VB.NET.
Here's what I have:
A ListView with checkboxes set to True
A Button (triggers the update)
A database table with similar fields as the ListView
What I want to happen:
when the user clicks the Button, all items on the ListView with checkbox checked will be updated.
My progress:
I've already collected the ID of the checked items and stored them in an array. I'll be using this to update the database table.
The problem:
I don't know how to put them in the SqlCommand.Parameters
Also, I don't know the update command for such scenario (where in/exist (#parameters))
Thanks in advance!
If you're using SQL Server 2008 or later, you can use table-valued parameters. These let you continue to deal with the separate IDs on separate rows, perform SQL joins, etc. There are plenty of examples on the page I've linked to, e.g.:
Using connection
' Create a DataTable with the modified rows.
Dim addedCategories As DataTable = _
CategoriesDataTable.GetChanges(DataRowState.Added)
' Define the INSERT-SELECT statement.
Dim sqlInsert As String = _
"INSERT INTO dbo.Categories (CategoryID, CategoryName)" _
& " SELECT nc.CategoryID, nc.CategoryName" _
& " FROM #tvpNewCategories AS nc;"
' Configure the command and parameter.
Dim insertCommand As New SqlCommand(sqlInsert, connection)
Dim tvpParam As SqlParameter = _
insertCommand.Parameters.AddWithValue( _
"#tvpNewCategories", addedCategories)
tvpParam.SqlDbType = SqlDbType.Structured
tvpParam.TypeName = "dbo.CategoryTableType"
' Execute the query
insertCommand.ExecuteNonQuery()
End Using
You could easily replace that INSERT with an UPDATE, as shown earlier on the page:
UPDATE dbo.Categories
SET Categories.CategoryName = ec.CategoryName
FROM dbo.Categories INNER JOIN #tvpEditedCategories AS ec
ON dbo.Categories.CategoryID = ec.CategoryID;
And adjust parameter names accordingly.
It seems you want to use a single call to the database, for that you would need to build a string with the command, I don't think you can pass a list as parameter to a command / sp.
The update command:
UPDATE [table] SET [field1] = value1, [field2] = value2
WHERE [ID] IN (id1, id2, ..., idN)