First I use System.Data.SQLite v1.0.111 to open a connection to in-memory database as below.
SQLiteConnection dest= new SQLiteConnection("FullUri='file:db1?mode=memory&cache=shared'");
dest.Open();
Then I have another sqlite database on file which is opened as below.
SQLiteConnection src= new SQLiteConnection(#"Data Source=C:\db1.sqlite");
src.Open();
Next, I tried to VACUUM INTO SQL to copy the file database into in-memory database, and it gave me an error.
using( SQLiteCommand cmd = src.CreateCommand() )
{
cmd.CommandText = $"VACUUM main INTO 'file:db1?mode=memory&cache=shared';";
cmd.ExecuteNonQuery();
}
SQLiteException: out of memory
at System.Data.SQLite.SQLite3.Reset(SQLiteStatement stmt)
at System.Data.SQLite.SQLite3.Step(SQLiteStatement stmt)
at System.Data.SQLite.SQLiteDataReader.NextResult()
at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery(CommandBehavior behavior)
at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()
The database in file is small(20KB), but it says out of memory?
Thank you
Well, first of all thanks for your Vacuum into syntax example, i wasn't able to find one!
here's the solution:
SQLiteConnection dest = new SQLiteConnection("FullUri='file:db1?mode=memory&cache=shared'");
dest.Open();
SQLiteConnection src = new SQLiteConnection(#"Data Source=MYDb.sqlite");
src.Open();
using (SQLiteCommand cmd = src.CreateCommand())
{
cmd.CommandText = #"VACUUM INTO 'file:db1?mode=memory&cache=shared';";
cmd.ExecuteNonQuery();
}
using (SQLiteCommand cmd = dest.CreateCommand())
{
cmd.CommandText = "SELECT count(*) FROM sqlite_master WHERE type = 'table'";
var ret = cmd.ExecuteScalar();
}
in my case ret was equal to 9 (number of tables in 'MYDb.sqlite').
I think the problem was in the vacuum into syntax: you should not specify the source db (VACUUM INTO not VACUUM main INTO).
If you use "Microsoft.Data.Sqlite" (lightweight ADO.NET provider for SQLite) here is an example of C# code that uses VACUUM:
using (SqliteConnection connection = new SqliteConnection("Data Source=hello.db"))
connection.Open();
SqliteCommand sqliteCommand = new SqliteCommand("VACUUM", connection);
sqliteCommand.ExecuteNonQuery();
}
Related
I have a database that was created with a password using the System.Data.Sqlite package and the ChangePassword method.
Is it possible to open this database using the Microsoft.Data.Sqlite package? I've tried using the sqlcipher package described here:
https://learn.microsoft.com/en-us/dotnet/standard/data/sqlite/encryption?tabs=netcore-cli
but I get the error:
Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 26: 'file is not a database'.
at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
at Microsoft.Data.Sqlite.SqliteCommand.<PrepareAndEnumerateStatements>d__64.MoveNext()
at Microsoft.Data.Sqlite.SqliteCommand.<GetStatements>d__54.MoveNext()
at Microsoft.Data.Sqlite.SqliteDataReader.NextResult()
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader()
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteNonQuery()
at Microsoft.Data.Sqlite.SqliteConnectionExtensions.ExecuteNonQuery(SqliteConnection connection, String commandText, SqliteParameter[] parameters)
at Microsoft.Data.Sqlite.SqliteConnection.Open()
My connection string looks like:
Data Source=<full path to db>;Password=<password>
More information:
Microsoft.Data.Sqlite.Core package 5.0.2
SQLitePCLRaw.bundle_e_sqlcipher package 2.0.4
I've also tried not specifying the password in the connection string but in a pragma statement after opening the connection string as described here:
https://www.bricelam.net/2016/06/13/sqlite-encryption.html
but I get the same error.
SqliteConnection liteConnection = new SqliteConnection(this.ConnectionString);
liteConnection.Open();
using (var command = liteConnection.CreateCommand())
{
command.CommandText = "SELECT quote($password);";
command.Parameters.AddWithValue("$password", _password);
var quotedPassword = (string)command.ExecuteScalar();
Console.WriteLine(quotedPassword);
command.CommandText = "PRAGMA key = " + quotedPassword;
command.Parameters.Clear();
command.ExecuteNonQuery();
}
Try to use cipher_migrate from SQLCipher. Maybe you have a different cipher version
connection.Open();
using var command = connection.CreateCommand();
command.CommandText = $"PRAGMA key = {DatabaseKey};PRAGMA cipher_migrate;";
command.Parameters.Clear();
command.ExecuteNonQuery();
This c# code just doesn't want to change Password on the server (UserID and PW will obviously be a strings for some purpose, but this is just to get it working):
SqlConnection conn = new SqlConnection ("Data Source=ServerIP;Persist Security Info=False;User ID=UserID;Password=UserPW");
SqlCommand cmd = new SqlCommand ("ALTER LOGIN UserID WITH PASSWORD='NewPW' OLD_PASSWORD='UserPW'", conn);
cmd.CommandType = CommandType.Text;
conn.Open();
conn.Close();
Changing Password using the same command ALTER LOGIN UserID WITH PASSWORD='NewPW' OLD_PASSWORD='UserPW' with a Server Management studio works like a charm, so there is no problem within command line or/and permissions for this particular User.
I guess I miss something in Sql Connection line.
Already tried combinations of:
Initial Catalog=master;
Initial Catalog=;
Integrated Security=SSPI;
Persist Security Info=True;
Changing command type, using ExecuteNonQuery();, and many other things, but nothing seems to work.
"Google" doesn't give any valuable result, hopefully I will find my answer here, thanks for taking your time in advance.
Try the following, works a treat for me:
SqlConnection conn = new SqlConnection ("Data Source=ServerIP;Persist Security Info=False;User ID=UserID;Password=UserPW");
SqlCommand cmd = new SqlCommand ("ALTER LOGIN UserID WITH PASSWORD='NewPW' OLD_PASSWORD='UserPW'", conn);
cmd.CommandType = CommandType.Text;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
You forgot to put cmd.ExecuteNonQuery() after you opened the connection. I just tested this and it successfully changed the password on my local database.
string queryString = #"DECLARE #sql NVARCHAR(500)
SET #sql = 'ALTER LOGIN ' + QuoteName(#loginName) +
' WITH PASSWORD= ' + QuoteName(#password, '''')
EXEC #sql ";
using (var connection = new SqlConnection(connectionString))
{
var command = new SqlCommand(queryString, connection);
command.Parameters.AddWithValue("#loginName", loginName);
command.Parameters.AddWithValue("#password", password);
connection.Open();
command.ExecuteNonQuery()
}
UPDATE
With DDL (data definition language) statements (as ALTER LOGIN) you cannot use parameters directly. That's why I'm using a dynamic SQL.
The QuoteName will do proper quoting in the SQL, simply doubles any [ characters (first call) or ' characters (second call).
I want to use MYSQL server with my asp.net application. But i am not able to connect to it. I am getting error that is "ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified " .
My code is:-
System.Data.Odbc.OdbcConnection cn = new System.Data.Odbc.OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=new_testdb; User=root;Password=abc123#;");
cn.Open();
System.Data.Odbc.OdbcCommand cmd = new System.Data.Odbc.OdbcCommand();
System.Data.Odbc.OdbcDataAdapter adp = null;
DataSet ds = new DataSet();
cmd.Connection = cn;
cmd.CommandText = "Select * from new_table";
adp = new System.Data.Odbc.OdbcDataAdapter(cmd);
adp.Fill(ds, "new_table");
this.GridView1.DataSource = ds;
this.GridView1.DataMember = "new_table";
cn.Close();
Try to download ADO.NET MySql Connector API (managed MySql Data provider) instead of ODBC Driver.
EDIT: Connector/NET Examples
You also may connect to MySQL with dotConnect for MySQL components.
Try to build the connection string with MySqlConnectionStringBuilder class.
This is what you need: http://dev.mysql.com/downloads/connector/net
Enjoy!
Could you please see here My SQL ConnectionString or ConnectionString
Click Left btn on database in server explorer, select properties copy connection string;
2.string c= persistsecurityinfo=True;server=localhost;user id=root;password=admin;database=sam;
MySqlConnection cn = new MySqlConnection(c);
cn.Open();
Response.Write("Connection successful !!");
MySqlDataAdapter Mda = new MySqlDataAdapter("select * from tblName", cn);
DataSet ds = new DataSet();
Mda.Fill(ds, "tblName");
GridView1.DataSource = ds.Tables["tblName"];
GridView1.DataBind();
Make sure that :-
using System.Data;
using MySql.Data.MySqlClient;
imported... :)
Hopefully this is not a ServerFault question...
I'm working forward on migrating a project from storing data in XML Serialization to a MySQL database. I'm using the example provided me from a previous question answered yesterday.
Connecting using phpMyAdmin and MySQL Workbench I've created a Stored Procedure called 'sprocOrderSelectSingleItem'. It seems to work well with MySQL for all I can tell. When I run the SHOW CREATE PROCEDURE sprocOrderSelectSingleItem it returns the following:
CREATE DEFINER=username#% PROCEDURE sprocOrderSelectSingleItem(IN orderID INTEGER)
BEGIN SELECT * FROM tblOrders WHERE ID=orderID; END
My cooperative ASP.NET code goes something like this:
public static Order GetItem(int ID)
{
Order objOrder = null;
using (OdbcConnection objConnection = new OdbcConnection(Utils.ApplicationConfiguration.ConnectionString))
{
OdbcCommand objCommand = new OdbcCommand("sprocOrderSelectSingleItem", objConnection);
objCommand.CommandType = CommandType.StoredProcedure;
objCommand.Parameters.AddWithValue("orderID", ID);
objConnection.Open();
using (OdbcDataReader objReader = objCommand.ExecuteReader())
{
if (objReader.Read())
{
objOrder = FillDataRecord(objReader);
}
objReader.Close();
}
objConnection.Close();
}
return objOrder;
}
When I view the page I get the following error message:
ERROR [42000] [MySQL][ODBC 5.1 Driver][mysqld-5.0.77]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'sprocOrderSelectSingleItem' at line 1
Really not catching on to what could be missing or going wrong. Are there any additional tests I should/could be running to confirm things are working on the MySQL side? Am I missing a step to pass the Stored Procedure call correctly in ASP.NET? The code breaks at the line of:
using (OdbcDataReader objReader = objCommand.ExecuteReader())
Replacing the line of
OdbcCommand objCommand = new OdbcCommand("sprocOrderSelectSingleItem", objConnection);
with this instead
OdbcCommand objCommand = new OdbcCommand("SELECT * FROM tblOrders WHERE ID=" + ID + ";", objConnection);
and everything works as expected.
Thanks for any help you guys can provide.
Your can run an execute on sprocOrderSelectSingleItem in Mysql directly with the ID parameter.
It will show that your StoredProc run correctly.
Here is a sample code in C# that call a stored proc.
OdbcCommand salesCMD = new OdbcCommand("{ CALL SalesByCategory(?) }", nwindConn);
salesCMD.CommandType = CommandType.StoredProcedure;
OdbcParameter myParm = salesCMD.Parameters.Add("#CategoryName", OdbcType.VarChar, 15);
myParm.Value = "Beverages";
OdbcDataReader myReader = salesCMD.ExecuteReader();
Look at the "Call" in the OdbcCommand and the "?" for the parameter that is later supplied with a value.
Can you try something like below:
OdbcCommand cmd = new OdbcCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "{call LoadCustCliOrders(?,?,?,?)}";
cmd.Parameters.Add("CUST_ID",OdbcType.Int);
cmd.Parameters.Add("CLIENT_ID",OdbcType.Int);
cmd.Parameters.Add("DATE_FROM",OdbcType.Date);
cmd.Parameters.Add("DATE_TO",OdbcType.Date);
...
cmd.Parameters["CUST_ID"].Value = _CustId;
cmd.Parameters["CLIENT_ID"].Value = _ClientId;
cmd.Parameters["DATE_FROM"].Value = _DateFrom;
cmd.Parameters["DATE_TO"].Value = _DateTo;
cmd.ExecuteReader
Are you sure that you are using the same username or user with the same access privileges.
I think you need to add the word "CALL" before the stored proc.
It should be CALL sprocOrderSelectSingleItem and try.
I have problem while connecting to mysql database using "ADO.NET Driver for MySQL (Connector/NET)"
I got this exception The given key was not present in the dictionary.
Edit:
MySqlConnection con = new MySqlConnection("Server=localhost;Database=pgs_db;Uid=root;Pwd=;");
MySqlCommand com = new MySqlCommand();
com.CommandType = CommandType.StoredProcedure;
com.Connection = con;
com.CommandText = "getStudent";
con.Open();
MySqlDataReader dr =com.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
con.Close();
sorry i was using a worng connectionstring
this is a right one :
"server=localhost; user id=user; password=password; database=mydatabase; pooling=false;"
thnx Oded
Without seeing the code, I am guessing you are trying to access a configuration key that does not exist in your application config file.
Edit:
After seeing the code sample, the problem is probably not with connecting to the DB.
I would say that in your GridView, you are binding to a field that is not returned by the DB query.