This is the delete function which i used to delete in sqlite database so i am seeing a exception database is locked.
Here's my Code :
Dim con As New SQLiteConnection("Data Source= " & AppSettings.DbName & ";Version=3;")
Try
con.Open()
Dim strQuery As String = "DELETE FROM Externalsupplier_Capablity WHERE ID = #id " ' WHERE Vendor_no = #VendorNo"
Dim cmd As SQLiteCommand = New SQLiteCommand(strQuery, con)
cmd.Parameters.Add("#id", DbType.String).Value = TxtdeleteID.Text
System.Diagnostics.Debug.Print(strQuery)
cmd.CommandType = CommandType.Text
cmd.Connection = con
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("An error has occurred: " & ex.Message)
Finally
con.Close()
con.Dispose()
End Try
Related
Imports System.Data.OleDb
Public Class Form1
Dim con As New OleDbConnection
Dim dbProvider As String = "Provider=Microsoft.ACE.OLEDB.12.0;"
Dim dbSource As String = "Data Source=D:\Visual Basic\dito\Database2.accdb;"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
con.ConnectionString = dbProvider & dbSource
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If TextBox3.Text = Nothing Or TextBox4.Text = Nothing Then
MessageBox.Show("Please enter details", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
If con.State = ConnectionState.Closed Then
con.Open()
End If
Using cmd As New OleDbCommand("SELECT COUNT(*)FROM member WHERE [USERNAME] = #USERNAME OR [PASSWORD] = #PASSWORD", con)
cmd.Parameters.AddWithValue("#USERNAME", OleDbType.VarChar).Value = TextBox3.Text
cmd.Parameters.AddWithValue("#PASSWORD", OleDbType.VarChar).Value = TextBox4.Text
Dim count = Convert.ToInt32(cmd.ExecuteScalar())
If count > 0 Then
MessageBox.Show("oops, username and password has already been taken", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
End Using
Using create As New OleDbCommand("INSERT INTO member(USERNAME], [PASSWORD])VALUES(#USERNAME,PASSWORD)")
create.Parameters.AddWithValue("#USERNAME", OleDbType.VarChar).Value = TextBox3.Text
create.Parameters.AddWithValue("#PASSWORD", OleDbType.VarChar).Value = TextBox4.Text
If create.ExecuteNonQuery Then
MessageBox.Show("Account created", "information", MessageBoxButtons.OK, MessageBoxIcon.Information)
TextBox3.Clear()
TextBox4.Clear()
End If
End Using
con.Close()
End Sub
End Class
pls help dont know what to do
I'm trying to get data from Oracle to a Datatable. My idea was to create a function to do all connection staff and then call it from other parts of the code.
Like:
dt = New DataTable
dt = Oracle2table(sql.ToString)
Then, I created the function:
Function Oracle2table(ByVal sql As String) As DataTable
Oracle2table = Nothing
Try
conn = New OleDb.OleDbConnection(connString)
conn.Open()
cmd = New OleDbCommand(sql, conn)
adaptador = New OleDbDataAdapter
adaptador.SelectCommand = cmd
adaptador.Fill(Oracle2table)
conn.Dispose()
Catch ex As Exception
MessageBox.Show("Error al cargar la tabla. ERROR = " + ex.ToString)
Finally
Oracle2table = Nothing
End Try
End Function
The problem with this code is initialize the variable (Oracle2table = nothing) so i didn't receive a function error.
I am not VB specialist, but this code worked fine, so it may be useful for you:
Dim dt = Oracle2table("select * from dual")
Console.WriteLine(dt.Rows(0)(0))
Function (I removed exception handling for simplicity):
Function Oracle2table(ByVal sql As String) As DataTable
Dim conn = New OleDbConnection(connString)
Oracle2table = New DataTable
conn = New OleDb.OleDbConnection(connString)
conn.Open()
Dim cmd = New OleDbCommand(sql, conn)
Dim adaptador = New OleDbDataAdapter
adaptador.SelectCommand = cmd
adaptador.Fill(Oracle2table)
conn.Dispose()
End Function
I have problem with running this:
i captured picture from my win
OleDbConnection con = new OleDbConnection();
DataTable dt = new DataTable();
con.ConnectionString = "Provider=MSDAORA;Data Source=DATA;Password=ss8_pakhsh;User ID=SHIFTS_N";
con.Open();
string sqlcommand = "INSERT INTO GROUPS(GROUP_ID, GROUP_NAME,DEPT_ID) " +
"VALUES(SEQ_MAX_GROUP_ID_NO.NEXTVAL,#groupName,SELECT
DEPT_ID FROM PERSONNEL_TEMP.DEPARTMENT WHERE
DEPARTMENT_NAME=#depName)";
OleDbCommand command = new OleDbCommand(sqlcommand, con);
command.Parameters.AddWithValue("#groupName", textBox1.Text);
command.Parameters.AddWithValue("#depName", comboBox1.SelectedItem);
OleDbDataAdapter oda = new OleDbDataAdapter(command);
oda.Fill(dt);
dataGridView2.DataSource = dt;
con.Close();
Change the sqlcommand string as shown below,
string sqlcommand = "INSERT INTO GROUPS(GROUP_ID, GROUP_NAME,DEPT_ID) " +
"VALUES(SEQ_MAX_GROUP_ID_NO.NEXTVAL,#groupName,SELECT "+
"DEPT_ID FROM PERSONNEL_TEMP.DEPARTMENT WHERE "+
"DEPARTMENT_NAME=#depName)";
This should solve the issue.
Observation: It is not a good practice to directly set the textBox1.Text value in parameter.
so this code works with just the out parameter but then when you add in parameters weird nulls start showing up and also the straight in parameters work too so it is just when there are both in and out parameters.
the part with problems is the in and out parameters do not want to coexist
cmd.Parameters.Add("PKEY", OracleDbType.Int64, ParameterDirection.Output)
For Each kvp As KeyValuePair(Of String, String) In sqlParams
Dim newParam As New OracleParameter(kvp.Key, OracleDbType.Varchar2, kvp.Value, Data.ParameterDirection.Output)
newParam.Size = 400
cmd.Parameters.Add(newParam)
Next
This works
Public Shared Sub Run_Oracle_Query(ByVal queryToRun As String, ByVal sqlParams As Dictionary(Of String, String))
Dim dbConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("ITSS").ConnectionString
Dim con As OracleConnection = New OracleConnection(dbConnString)
Dim cmd = con.CreateCommand()
Try
con.Open()
cmd.CommandText = queryToRun
cmd.CommandType = CommandType.Text
For Each kvp As KeyValuePair(Of String, String) In sqlParams
cmd.Parameters.Add(kvp.Key, kvp.Value)
Next
cmd.ExecuteNonQuery()
'log all sql queryies very expensive operation
LogThisString("Log All queries: " & queryToRun, "Always")
Catch ex As OracleException ' catches only Oracle errors
OracleExceptionLogging(ex.Number, queryToRun, ex)
Catch ex As Exception
LogThisString("General Error SQL: " + ex.Message.ToString(), "Always")
' MsgBox("Could Not Perform This Database Operation")
Finally
con.Close()
cmd.Dispose()
con.Dispose()
End Try
End Sub
This works
Public Shared Function Run_Insert_Oracle_Query_Return_ID(ByVal queryToRun As String, ByVal IDcolumnName As String) As String
Dim dbConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("ITSS").ConnectionString
Dim con As OracleConnection = New OracleConnection(dbConnString)
Dim cmd = con.CreateCommand()
Dim strUkey As String = ""
Try
con.Open()
cmd.CommandText = queryToRun & " RETURNING " & IDcolumnName & " INTO :UKEY"
cmd.CommandType = CommandType.Text
cmd.Parameters.Add("PKEY", OracleDbType.Int64, ParameterDirection.Output)
cmd.ExecuteNonQuery()
strUkey = cmd.Parameters("PKEY").Value.ToString
Return strUkey
Catch ex As OracleException ' catches only Oracle errors
OracleExceptionLogging(ex.Number, queryToRun, ex)
Catch ex As Exception
LogThisString("General Error SQL: " + ex.Message.ToString(), "Always")
PopupMsgBox("Database Error", "Database Integrity Constants Violated SQL Operation Will Not Be Executed")
Finally
con.Close()
cmd.Dispose()
con.Dispose()
End Try
Return strUkey
End Function
This does not work
Public Shared Function Run_Insert_Oracle_Query_Return_ID(ByVal queryToRun As String, ByVal IDcolumnName As String, ByVal sqlParams As Dictionary(Of String, String)) As String
Dim dbConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("ITSS").ConnectionString
Dim con As OracleConnection = New OracleConnection(dbConnString)
Dim cmd = con.CreateCommand()
Dim strUkey As String = ""
Try
con.Open()
cmd.CommandText = queryToRun & " RETURNING " & IDcolumnName & " INTO :UKEY"
cmd.CommandType = CommandType.Text
cmd.Parameters.Add("PKEY", OracleDbType.Int64, ParameterDirection.Output)
For Each kvp As KeyValuePair(Of String, String) In sqlParams
Dim newParam As New OracleParameter(kvp.Key, OracleDbType.Varchar2, kvp.Value, Data.ParameterDirection.Output)
newParam.Size = 400
cmd.Parameters.Add(newParam)
Next
cmd.ExecuteNonQuery()
strUkey = cmd.Parameters("PKEY").Value.ToString
Return strUkey
Catch ex As OracleException ' catches only Oracle errors
OracleExceptionLogging(ex.Number, queryToRun, ex)
Catch ex As Exception
LogThisString("General Error SQL: " + ex.Message.ToString(), "Always")
PopupMsgBox("Database Error", "Database Integrity Constants Violated SQL Operation Will Not Be Executed")
Finally
con.Close()
cmd.Dispose()
con.Dispose()
End Try
Return strUkey
End Function
crap so i guess it does look for order asnd the output does need to be at end like this.....this works solved my own problem
For Each kvp As KeyValuePair(Of String, String) In sqlParams
Dim newParam As New OracleParameter(kvp.Key, OracleDbType.Varchar2, kvp.Value, Data.ParameterDirection.Input)
newParam.Size = 400
cmd.Parameters.Add(newParam)
Next
cmd.Parameters.Add("PKEY", OracleDbType.Int64, ParameterDirection.Output)
am trying to develop a c# standalone application and my insert code has no error but when i click it will insert to first table or crtPro perfectly but it didn't add to the second table , can anyone give me some hint...???
here is my code for insert code...
private void button1_Click(object sender, EventArgs e)
{
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
conn.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;" +
#"Data source= C:\Documents and Settings\abel\My Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\crt_db.accdb";
try
{
conn.Open();
String Name = txtName.Text.ToString();
String Address = txtAddress.Text.ToString();
//String Wereda = txtWereda.Text.ToString();
//String Kebele = txtKebele.Text.ToString();
//String House_No = txtHouse.Text.ToString();
//String P_O_BOX = txtPobox.Text.ToString();
String Tel = txtTel.Text.ToString();
//String Fax = txtFax.Text.ToString();
String Email = txtEmail.Text.ToString();
String Item = txtItem.Text.ToString();
String Dep = txtDep.Text.ToString();
String Status = ToString();
String Remark = txtRemark.Text.ToString();
String Type = txtType.Text.ToString();
String Brand = txtBrand.Text.ToString();
String License_No = txtlicense.Text.ToString();
String Date_issued = txtDate.Text.ToString();
String my_querry = "INSERT INTO crtPro(Name,Address,Email,Tel,Item,Dep,Status,Remark)VALUES('" + Name + "','" + Address + "','" + Email + "','" + Tel + "','" + Item + "','" + Dep + "','" + Remark + "')";
OleDbCommand cmd = new OleDbCommand(my_querry, conn);
cmd.ExecuteNonQuery();
conn.Close();
conn.Open();
String my_querry1 = "SELECT LAST(PID) FROM crtPro";
OleDbCommand cmd1 = new OleDbCommand(my_querry1, conn);
string var = cmd1.ExecuteScalar().ToString();
//txtStatus.Text = var;
String PID = ToString();
String my_querry2 = "INSERT INTO crtItemLicense(PID,Type,Brand,License_No,Date_issued)VALUES('" +PID + "','" + Type + "','" + Brand + "','" + License_No + "','" + Date_issued + "')";
OleDbCommand cmd2 = new OleDbCommand(my_querry2, conn);
cmd2.ExecuteNonQuery();
MessageBox.Show("Message added succesfully");
}
catch (Exception ex)
{
MessageBox.Show("Failed due to" + ex.Message);
}
finally
{
conn.Close();
}
}
There are a couple of calls like:
String PID = ToString();
That will try to get a string representation of 'this', which is your form. That is probably not what you want to be inserting into the database. You probably meant
String PID = var.ToString();
If that isn't it, then try putting more tracewrites (or using a debugger) to narrow the problem down to find which one of your sql statements is causing the problem.
Also once you have it working, try entering this in the name field of your form:
Robert"); DROP TABLE crtPro;--
and then do some googling about SQL injection (apologies to XKCD).