I am trying to make a SQLite SQL query using parameters and for some reason its not working (i have done this with an OLE db before successfully). I took the below syntax from a google search but doesnt seem to be working.
It doesnt seem to like my command.perameter.add line and its giving me a "SQLite is not accessible due to its protection level" error.
I imagine its me converting from OLE to SQLlite syntax that is causing the issue but im not sure. Any ideas? My code is as below:
string sql = "INSERT INTO Jobs (jEnquiryNumber) VALUES (#jEnquiryNumber)";
MessageBox.Show(sql);
SQLiteCommand command = new SQLiteCommand(sql, dbConnectString);
using (command)
{
command.Parameters.Add("#jEnquiryNumber", SQLiteType.Text).Value = textBox1.Text;
}
Related
I've created an App and have created the Azure database which I can connect to and populate with no issues.
I have a SQL query that I have hard coded in to a controller and view using a Data Reader, as there's a Pivot and it was the only way I could get it to work correctly.
con.Open();
com.Connection = con;
com.CommandText = "SELECT * FROM [DatabaseName].[dbo].[TABLE]"
dr = com.ExecuteReader();
while (dr.Read())
{
// Assign the variables
}
[DatabaseName].[dbo].[TABLE] This is the part where I'm having issues, throwing an invalid Object name for the Table, it works locally but not when trying to connect to Azure.
Can someone help me with a fix please?
Many thanks
Cross-database and cross-instance queries using three or four part names are not supported in Azure Sql Database. The differences in TSQL are documented here https://learn.microsoft.com/en-us/azure/azure-sql/database/transact-sql-tsql-differences-sql-server?view=azuresql
I have a c# winform project that supposes to save data to SQLite database, I've already used the dll properly and it runs without error, but I get an exception when trigger the method with buttonClick event
here the exception i got : Keyword not supported :'version'.
this the connection string:
"Data Source = Diary.db;Version = 3;New = False;Compress = True;";
and this the complete method :
private void AddToDbaseSQL3()
{
try{
string query = "insert into Diary(title,date,mood,wheater,content)
values('"+TitleTextbox.Text+"','"
+dateTimePicker.Value.Date.ToString("yyyy-MM-dd HH:mm")+"','"
+MoodCombobox.SelectedItem+"','"
+WheaterCombobox.SelectedItem+"','"
+ContentTextbox.Text+"');";;
SqlConnection connect2 = new SqlConnection(connection2);
SqlCommand cmd = new SqlCommand(query,connect2);
SqlDataReader read;
connect2.Open();
read = cmd.ExecuteReader();
while(read.Read())
{
}
MessageBox.Show("created");
TitleTextbox.Text = "Title";
TitleTextbox.ForeColor = SystemColors.ControlLight;
ContentTextbox.Clear();
connect2.Close();
}catch(Exception e){
MessageBox.Show(e.Message);
}
}
I've looked to this link:
Keyword not supported: 'version'
and it said to change SqlConnection to SQLiteConnection but it ended with an error, can you tell what's is the right connection string ? or there is something wrong from my code/method? please tell me, thank you, I'm sorry because it's my first time using the SQLite
SqlConnection from System.Data.SqlClient is for SQL Server.
You need an dedicated SQLite ADO.NET provider.
You can found the System.Data.SQLite provider from SQLite team here:
https://system.data.sqlite.org
Or you can use any libre or commercial provider.
You can also use the free and open-source SQLite ODBC driver that works fine and allow to use VS Visual Designers to create strongly typed ADO.NET DataSets, in addition to the use of OdbcConnection, OdbcCommand and so on:
http://www.ch-werner.de/sqliteodbc
C# Reading data from existing SQLite database
I had implemented a database storage on my app for Windows Mobile 6.5 using SQL Server CE.
Had managed to install the SQL Server CE CAB file on the device (Motorola MC65).
Managed to create database file, and create tables. Insert also can be executed.
However when I try to run ExecuteReader() to read records, I hit the following error:
Error Code: 80004005
Message : Unspecified error
Minor Err.: 25534
Source : SQL Server Compact ADO.NET Data Provider
No idea why this is happening. Since insert can be executed I thought this should not be a connection or privilege issue.
The reading code is as below:
openConnection();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT NAME FROM GROUP_INFO ORDER BY NAME ";
SqlCeDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
string groupName = reader.GetString(0);
listGroup.Add(groupName);
}
The exception is thrown on the line where cmd.ExecuteReader() is executing.
Any pointers are appreciated. Thanks.
Implement proper error handling for SqlCeExceptions! The error is documented here https://technet.microsoft.com/en-us/library/ms172350(v=sql.110).aspx
Large objects (ntext and image) cannot be used in ORDER BY clauses.
Maybe you should redefine the column as nvarchar(4000) (it is currently ntext), or rephrase the query using:
ORDER BY CAST(Name as nvarchar(4000))
But this will cause a table scan
I am trying to use a parametrized query to create an index on our Oracle server. I can create the index fine if I use string concatenation, so I believe its not an account or permissions issue. I get the error:
ORA-01036: illegal variable name/number
I can not find any error in the code, but I am sure I am missing something. I am using Oracle.DataAccess.dll version 4.112.3.0. The version the server reports is "Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production"
I have tried removing all the params, except one and get the same error. Any combination of 1 or more params causes the error. The error happens every time without fail. I have used watches to verify that the params are correctly getting set, and I have tried clearing the params before setting. No matter what I do so far, I still get the error. Here is the code:
using (OracleCommand mycom = new OracleCommand())
{
using (OracleConnection Connection = new OracleConnection(connectionString))
{
mycom.BindByName = true;
mycom.CommandText = "CREATE INDEX :indexname on :tablename (:colname)";
mycom.Parameters.Add("indexname", indexName);
mycom.Parameters.Add("tablename", tableName);
mycom.Parameters.Add("colname", colName);
mycom.Connection = Connection;
mycom.CommandType = CommandType.Text;
mycom.Connection.Open();
mycom.ExecuteNonQuery();
}
}
You cannot use bind variables in a DDL statement (like CREATE INDEX). Bind variables are only used in DML statements. You would need to build up the DDL statement in your C# code.
Is this a part of the installation process for your application? Otherwise, it would be exceedingly unusual to want to create an index from an application. Your application shouldn't be creating any database objects outside of the initial installation process.
I am trying to execute a sql query as another login using the 'Execute As' command. I am using Linq to SQL, so I've generated a Data Context class and I am using the ExecuteQuery method to run the 'Execute As' SQL command. I then call a Linq to SQL command that is successful. However, every subsequent query fails with the following error:
A severe error occurred on the current command. The results, if any, should be discarded.
Here is the code snippet that I have tried:
SummaryDataContext summary = new SummaryDataContext();
summary.ExecuteQuery<CustomPostResult>(#"Execute as Login='Titan\Administrator'");
var test = summary.Customers.First();
var test2 = summary.Products.ToList();
No matter what query I run on the second query I receive the error message from above. Any help would be appreciated.
I managed to get around this issue in my application by executing the query using ADO.NET classes.
SqlCommand cmd = new SqlCommand("EXECUTE AS USER = 'operator'");
cmd.Connection = dc.Connection as SqlConnection;
cmd.Connection.Open();
cmd.ExecuteNonQuery();
// do the rest of the queries using linq to sql
You may have already ruled this out, but one possible work around would be to simply create the data context with a different connection string.
To edit the connection string, you can set the DataContext.Connection.ConnectionString property. I've done it before in the partial method OnCreated(), which gets called when the data context gets created. I haven't tested but I think you could also do:
YourDataContext dc = new YourDataContext();
dc.Connection.ConnectionString = "connection string here";
Here's an article that describes this as well - http://www.mha.dk/post/Setting-DataContext-Connection-String-at-runtime.aspx
I was having a similar issue and by looking at ruskey's answer I was able to Execute as User but noticed that I was getting errors when running other queries after that. It was due to the missing Revert. So for anyone having a similar issue this is how the code looks like.
SqlCommand cmd = new SqlCommand("EXECUTE AS USER = 'domain\\user';");
OSSDBDataContext dc = new OSSDBDataContext();
cmd.Connection = dc.Connection as SqlConnection;
cmd.Connection.Open();
cmd.ExecuteNonQuery();
//Execute stored procedure code goes here
SqlCommand cmd2 = new SqlCommand("REVERT;");
cmd2.Connection = dc.Connection as SqlConnection;
cmd2.ExecuteNonQuery();