I am having trouble in getting my database updated using my C# form. I am using two richtextboxes, one that will be read only and will contain data that has been retrieved from my database (richTextBox). The other richtextbox will be used to allow for data input into the database (richTextBox1). When my form loads, the data from the database is loaded int开发者_JS百科o the read only textbox. Data can then be written into richTextBox1 that will then be appended to the database once I have pressed the 'Edit' button.
The problem I am having is that instead of appending the data to the database, the new data entered is overwriting the old data. It seems from the sources I have read, that there is no way of appending data, only replacing data. My code is as follows:
Code to retrieve data from database:
private void QuizForm_Load(object sender, EventArgs e)
{
//declare connection string using windows security
string cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Hannah\\Desktop\\Quiz.accdb";
//declare Connection, command and other related objects
OleDbConnection conGet = new OleDbConnection(cnString);
OleDbCommand cmdGet = new OleDbCommand();
try
{
//open connection
conGet.Open();
cmdGet.CommandType = CommandType.Text;
cmdGet.Connection = conGet;
cmdGet.CommandText = "SELECT DataToRead FROM QuizQuestions";
richTextBox.Text = cmdGet.ExecuteScalar().ToString();
conGet.Close();
}
Code to save data to database:
private void btnEdit_Click(object sender, EventArgs e)
{
//richTextBox.Enabled = true;
{
//declare connection string using windows security
string cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Hannah\\Desktop\\Quiz.accdb";
//declare Connection, command and other related objects
OleDbConnection conSave = new OleDbConnection(cnString);
OleDbCommand cmdSave = new OleDbCommand();
try
{
//open connection
conSave.Open();
cmdSave.CommandType = CommandType.Text;
cmdSave.Connection = conSave;
cmdSave.CommandText = "UPDATE QuizQuestions SET DataToRead = @prichTextBox";
OleDbParameter parrichTextBox1 = new
OleDbParameter("@prichTextBox", OleDbType.VarChar);
parrichTextBox1.Value = (richTextBox.Text + richTextBox1.Text);
cmdSave.ExecuteNonQuery();
//cmdSave.Parameters.Add("@parrichTextBox1", OleDbType.VarChar, combinedTextBox);
conSave.Close();
//END OF MY CODE
}
In summary, this is what I would like to happen: 1. The form opens, and automatically the richTextBox is populated with the database contents 2. Data/text is entered in richTextBox1, the 'Edit' button is pressed, resulting in that data being appended to the database. 3. When the form is reopened, the data added is visible in the read-only textbox richTextBox
Your SQL statement is indeed overwriting the data. If you'd like to append the data that exists in the richTextBox to the record that has been read from the DB, your UPDATE SQL statement should look like this:
UPDATE QuizQuestions SET DataToRead = DataToRead + @prichTextBox
What happens if you change your update statement to this instead:
cmdSave.CommandText = "UPDATE QuizQuestions SET DataToRead = DataToRead + ' '" + @prichTextBox";
精彩评论