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?
Related
I'm working on Windows CE application, I was trying to connect to server database from the device and fetch some information from db on button click, below is the code I tried,
SqlConnection conn = new SqlConnection("Data Source=192.168.0.0;Initial Catalog=DashReport;Integrated Security=SSPI; User ID=SA;Password=Admin#123;");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT STORE, YEAR,DATE FROM TOPSALES WHERE MONTH = " + txtcode.Text + ";";
// cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
conn.Open();
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
gvDataGrid.DataSource = dt;
}
else
{
MessageBox.Show("No data found");
}
}
conn.Close();
but while running the application I'm getting a SqlException. It seems there is something wrong with the connection string. What is the right method to do it?
You cannot have both the integrated security and specify a specific user id and password at the same time. Since you have the Integrated Security=SSPI;, that will take precedence and your connection tries to connect with the currently logged in Windows user.
Most likely, from a Windows CE device, you want to use the specific User I
string connStr = "Data Source=192.168.0.0;Initial Catalog=DashReport;User ID=SA;Password=Admin#123;"
SqlConnection conn = new SqlConnection(connStr);
And another word of caution from long time SQL Server users, admins, and programmers: you should NEVER EVER use the built-in sa account! Just don't do it - use another account (possibly one you create specifically for this application).
Have you tried using the Server Explorer of Visual Studio, then connect to the database, get the Connection String via Properties Window, and use it as your connection string?
Just some kind of assurance that your connection to the database is the same as your code.
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();
I'm currently trying to develop an application that allows you to track your spending as part of my class. However I run into the error "Invalid object name 'dbo.AccTransactions"
My Windows Form Code:
string command = "Insert INTO dbo.AccTransactions (id, transact_id, payee, dateof, amount, category)"
+ "Values (#id, #transact_id, #payee, #dateof, #amount, #category)";
SqlCommand cmd = new SqlCommand(command, con);
cmd.Parameters.AddWithValue("#id", 1);
cmd.Parameters.AddWithValue("#transact_id", 2);
cmd.Parameters.AddWithValue("#payee", payeeTextBox.Text);
cmd.Parameters.AddWithValue("#dateof", DateTime.Today);
cmd.Parameters.AddWithValue("#amount", Convert.ToDecimal(amountTextBox.Text));
cmd.Parameters.AddWithValue("#category", categoryTextBox.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
My Connection String:
SqlConnection con = new SqlConnection(#"Data Source=IVY\SQLEXPRESS;Initial Catalog=BudgetTracker;Integrated Security=SSPI;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False");
My database has the table "dbo.AccTransactions" and it performs perfectly in SSMS.
Any assistance would be great!
I guess you are missing database name in the connection string.
SqlConnection con = new SqlConnection(#"Data Source=IVY\SQLEXPRESS;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False");
try it by putting the name of your database in the following string.
SqlConnection con = new SqlConnection(#"Data Source=IVY\SQLEXPRESS; Initial Catalog=YOUR_DATABASE_NAME;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False");
Please check that user who is running your program has an access to that table. Most probably you need to set your user as dbo since you accessing the table as dbo.AccTransactions.
I developed an application. It loads sql database on my pc with this connection string:
Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Database\Books.mdf;Integrated Security=True;User Instance=True
private void Window_Loaded(object sender, RoutedEventArgs e)
{
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Database\Books.mdf;Integrated Security=True;User Instance=True");
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("SELECT * FROM Lessons", con);
da.Fill(ds);
grdPersonnel1.DataContext = ds.Tables[0];
con.Open();
}
but, my Database data doesn't load in another pc!
Do you have SQL server instance Running on that Computer?
Try to Run your Application/Solution on the other P.C on Debug Mode you will see what exactly is the error...make sure you have try and catch in each of your methods/ events.
check this SO post :
Is it possible to run a mdf database without SQL Server program? (c#)
Connecting to sql server database mdf file without installing sql server on client machine?
Regards
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