I want to publish my project with compact database but I have an error message that says: the path is not exists, I solved it by write this code:
SqlCeConnection connection = new SqlCeConnection("Data Source=|DataDirectory|\\Database1.sdf");
but I don't have result, I mean no delete no select no insert etc..
this my delete code:
SqlCeCommand cmd = new SqlCeCommand("delete from tab ", connection);
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
Related
When I run my project in the Unity Editor, I can run my database. I can read and write to it with no issues. However when I build my project and run it, at the point where my database is read nothing happens.
I have set it up to create a text file when the database is read to check if it is read, but the text file is not created. From this I can assume that the database connection wasn't opened.
I am using a MySQL database that is in LocalHost.
Is there a way of putting a database in the project files so that is accessible wherever the program is (e.g. on another computer), and to make this database readable? As if it were a textfile. This would be all I need. The database has a number of tables and all of them are accesed from my C# scripts when the game is running.
I do have a password for the connection but I haven't included it in the code below.
I need a really simple solution because data is only needed locally for the player's progress to be stored so there doesn't need to be a server hosting it if possible.
public string location = #"server=localhost;user=root;database=wattsup;port=3306;Allow User Variables=True"; //Path to database
MySqlConnection con = new MySqlConnection(location);
con.Open();
MySqlCommandBuilder cmdBuilder = new MySqlCommandBuilder();
string tbname = cmdBuilder.QuoteIdentifier(name1);
string query = "SELECT Question FROM " + tbname + " WHERE ID = #value";
MySqlCommand command = new MySqlCommand(query, con);
command.Parameters.AddWithValue("#value", Equations[EquationValue].EquationID);
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
SpaceGame.question = Convert.ToString(reader[0]);
string fileName = #"Question.txt";
//text file created to show me whether it is opened or not
using (StreamWriter fileStream = File.CreateText(fileName))
{
fileStream.WriteLine(SpaceGame.question);
}
}
con.Close();
If you want to store the data in single file, maybe you can try SQLite. The following is the steps that using SQLite you can refer to.
1.Install System.Data.SQLite using NuGet Package Manager.
2.Configure the connection string in App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
...
<connectionStrings>
<add name="SQLiteDbContext" connectionString="Data Source=MyDatabase.sqlite" providerName="System.Data.SQLite.EF6" />
</connectionStrings>
</configuration>
3.The simple demo that create .sqlite.
// create
SQLiteConnection.CreateFile("MyDatabase.sqlite");
SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite");
m_dbConnection.Open();
string sql = "create table highscores (name varchar(20), score int)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
sql = "insert into highscores (name, score) values ('Me', 9001)";
command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();// read
SQLiteCommand sqlCom = new SQLiteCommand("Select * From highscores", m_dbConnection);
SQLiteDataReader sqlDataReader = sqlCom.ExecuteReader();
int i = 1;
while (sqlDataReader.Read())
{
listBox1.Items.Add(i);
listBox1.Items.Add(sqlDataReader.GetValue(0));
listBox1.Items.Add(sqlDataReader.GetValue(1));
i++;
}
m_dbConnection.Close();
I want to create a program for my school, handling marks and certificates.
To save the data I have to use a "local" network shared database-file, because we are using Citrix and there is no possibility to setup an seperate SQL-Server.
I tried it with localdb v11 with the following code:
string connectionString = #"Data Source=(LocalDB)\v11.0; AttachDbFilename=D:\_prog\TestNoten\TestNoten\bin\Debug\Database1.mdf; Integrated Security=True;Connect Timeout=3;";
SqlConnection connection = new SqlConnection(connectionString);
string sql = "INSERT INTO test(name) VALUES('lal')";
SqlCommand cmd = new SqlCommand(sql, connection);
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
DataTable table = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter("Select * from test", connection);
connection.Open();
adp.Fill(table);
MessageBox.Show(table.Rows[0][1].ToString());
The MessageBox says "lal", but when I restart the program with "lala" instead of "lal", it will show "lala" and not the expected "lal".
So when closing the program, the database won't be saved correctly. I also opened the file over VSs Data Connections, and the testing table is empty.
Is there something I missed?
I am trying implement a SQL INSERT command into my MS Access database using C# with Visual Studio 2012.
But after that, when I open my Access database, there is no updates even the insert command can be successfully created by showing the success pop-up window after executeNonQuery()
Would you please tell me how to make this insert into SQL command work?
This is my code
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = #"INSERT INTO STUDENT(CHI_NAME, ENG_NAME, NICK_NAME,
BD_YYYY, BD_MM, BD_DD,
HOME_TEL, NO_OF_CHILD, PARENT_EMAIL,
SEC_EMAIL, STUDENT_ADDR, DISTRICT,
MOTHER_NAME, MOTHER_TEL, MOTHER_OCCU,
FATHER_NAME, FATHER_TEL, FATHER_OCCU,
E_NAME, E_TEL, E_RELATIONSHIP, REMARKS)
VALUES(#NEW_CHI_NAME, #NEW_EN G_NAME, #NEW_NICK_NAME,
#NEW_BD_YYYY, #NEW_BD_MM .....);"
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#NEW_CHI_NAME", chi_name);
cmd.Parameters.AddWithValue("#NEW_ENG_NAME", eng_name);
cmd.Parameters.AddWithValue("#NEW_NICK_NAME", nick_name);
cmd.Parameters.AddWithValue("#NEW_BD_YYYY", bd_year);
cmd.Parameters.AddWithValue("#NEW_BD_MM", bd_month);
....
cmd.Connection = connection;
connection.Open();
cmd.Transaction = connection.BeginTransaction();
int rows= cmd.ExecuteNonQuery();
if(rows > 0)
{
MessageBox.Show("Insert New Client Success!");
}
else
{
MessageBox.Show("Insert New Client Failed!");
}
cmd.Transaction.Commit();
connection.Dispose();
connection.Close();
In Solution Explorer, right click your Access file, choose Properties, and set Copy To Output Directory to Copy if Newer
Go to the debug directory, you will find you accdb file copied there. Open it and verify.
When I tried to add a connection it is showing the following error as shown in the attachment. “Unable to open the physical file. Access is Denied” .
When I searched about it, it suggest for adding the SQL Server’s account to the folder. Then, using the following query I found that the account is “LocalSystem”. When I tried to add “LocalSystem” to ACL of the folder, such an account is not available. How do we resolve it and add the connection to DBML?
Note: When I used DataReader with the database name in a C# program, it worked well.
Query Used:
declare #sqlser varchar(20)
EXEC master..xp_regread #rootkey='HKEY_LOCAL_MACHINE',
#key='SYSTEM\CurrentControlSet\Services\MSSQLSERVER',
#value_name='objectname', #value=#sqlser OUTPUT
SELECT convert(varchar(30),#sqlser)
Working C# Program:
SqlDataReader rdr = null;
SqlConnection con = null;
SqlCommand cmd = null;
try
{
// Open connection to the database
string ConnectionString = "server=D088DTRV;integrated security=true; database=BankAccount";
con = new SqlConnection(ConnectionString);
con.Open();
string CommandText = "SELECT * FROM Account";
cmd = new SqlCommand(CommandText);
cmd.Connection = con;
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
string test = rdr["AccountType"].ToString();
}
}
The problem was related to Data Connections.
In the advanced window, when I checked, it was trying for ./SQLExpress. I modified it with ".".
I restarted the machine. I also stopped the SQLExpress in the services.msc
Data Source=.;AttachDbFilename=C:\DevTEST\Databases\LibraryReservationSystem.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True
I was created a .mdf database file with vs 2010. I can retrive and insert data into database bbut when i want to backup error was handled. that database not attach in Management Studio.
my Code :
SqlConnection connect;
connect = new SqlConnection(DAL.AccessLayerClass._connectionStr);
connect.Open();
SqlCommand command;
command = new SqlCommand(#"backup database AGMDB to disk ='d:\svBackUp1.bak' with init,stats=10",connect);
command.ExecuteNonQuery();
connect.Close();
MessageBox.Show("The support of the database was successfully performed", "Back", MessageBoxButtons.OK, MessageBoxIcon.Information);
my connection string:
string _connectionStr = "Data Source=.\\SQLEXPRESS; AttachDbFilename=" + System.Windows.Forms.Application.StartupPath + "\\Database\\AGMDB.mdf; Integrated Security=True; Connect Timeout=30; User Instance=True;";
and that error was occurs:
Could not locate entry in sysdatabases for database 'AGMDB'. No entry found with that name. Make sure that the name is entered correctly.
BACKUP DATABASE is terminating abnormally.
how can i solve this error?
Thanks
You need [] around DBFileName, try this:
SqlConnection connect;
connect = new SqlConnection(DAL.AccessLayerClass._connectionStr);
connect.Open();
SqlCommand command;
command = new SqlCommand(#"backup database [" + System.Windows.Forms.Application.StartupPath + "\\Database\\AGMDB.mdf] to disk ='d:\svBackUp1.bak' with init,stats=10",connect);
command.ExecuteNonQuery();
connect.Close();
MessageBox.Show("The support of the database was successfully performed", "Back", MessageBoxButtons.OK, MessageBoxIcon.Information);
Reference : How to Backup and Restore SQL Express 2005 (AttachDbFilename mode)