I tried to read *.rtf files from an database, but I couldn't.
Here is the code which I used to add text in database from richTextBox
SqlConnection con = new SqlConnection(#"Data Source=MARIA-PC;Initial Catalog=Account;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO [dbo].[FILE] (File_name,The_text) VALUES (#File_name,#The_text)", con);
cmd.Parameters.Add("#File_name", textBox1.Text);
cmd.Parameters.AddWithValue("#The_text", richTextBox1.Rtf);
cmd.ExecuteNonQuery();
con.Close();
And the table I created
Click here
I tried a lot of solutions I found on the internet, but none of them where any good.
What I want to do is to display the text saved in database in the blank richTextBox.
Related
I'm trying to insert data into two tables via a WPF app with a button that saves the data, but I keep getting this error and I need some help. I've tried many videos, try to change the tables in the database but I can't get it to work but it works on the Viestit table, but not on Viestihistoria. The error is
System.Data.SqlClient.SqlException: 'Incorrect syntax near '#Viestinlahettaja'
Thank you in advance.
SqlConnection conn = new SqlConnection(#"Data Source=MYCOMPUTER\SQLEXPRESS;Initial Catalog=LomakeDB;User ID=***;Password=***");
conn.Open();
SqlCommand cmd1 = new SqlCommand("INSERT INTO Viestit(ID,Kokonimi,Viesti,Viestin_saaja) values(#ID,#Kokonimi,#Viesti,#Viestin_saaja)", conn);
cmd1.Parameters.AddWithValue("#ID", txtbox_ID.Text);
cmd1.Parameters.AddWithValue("#Kokonimi",txtbox_Nimi.Text);
cmd1.Parameters.AddWithValue("#Viesti", txtbox_Viesti.Text);
cmd1.Parameters.AddWithValue("#Viestin_saaja", txtbox_Viestinvastaanottaja.Text);
cmd1.ExecuteNonQuery();
cmd1.Parameters.Clear();
SqlCommand cmd2 = new SqlCommand("INSERT INTO Viestihistoria(ViestiID,Viesti,Viestin_lahettaja) values(#ViestiID,#Viesti2,#Viestinlahettaja", conn);
cmd2.Parameters.AddWithValue("#ViestiID", txtbox_ID.Text);
cmd2.Parameters.AddWithValue("#Viesti2", txtbox_Viesti.Text);
cmd2.Parameters.AddWithValue("#Viestinlahettaja", txtbox_Nimi.Text);
cmd2.ExecuteNonQuery();
cmd2.Parameters.Clear();
MessageBox.Show("Data lähetetty onnistuneesti!");
conn.Close();
You forgot to add closing ) at the end of this line in sql command text:
The corrected line:
SqlCommand cmd2 = new SqlCommand("INSERT INTO Viestihistoria(ViestiID,Viesti,Viestin_lahettaja) values(#ViestiID,#Viesti2,#Viestinlahettaja)", conn);
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?
This is my code:
conn = new SqlConnection("Server=(localdb)\\v11.0;Integrated Security=true;AttachDbFileName=|DataDirectory|\\Users.mdf;MultipleActiveResultSets=True;");
conn.Open();
SqlCommand comm = new SqlCommand("update users set surname='simpson' where id=1", conn);
int i = comm.ExecuteNonQuery();
MessageBox.Show(i + "");
comm = new SqlCommand("select surname from users where id=1", conn);
SqlDataReader reader = comm.ExecuteReader();
if (reader.Read())
MessageBox.Show(reader[0] + "");
conn.Close();
The ExecuteNonQuery returns 1 to show the database has been updated and the second query confirms it. But when I open the database in visual studio 2013, there are no changes, database still the same
YOu mean, when you stop the program and Visual Studio throws away the copy of the database and resets it to the empty one then the data is gone? Yes, that is true.
Maybe you need to change your connectionstring, or refresh the database.
Apparently the database had been set to copy to the output folder each time
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.
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