C# 不知道如何在数据库中添加值
我已经获得了工作输入,但现在每次输入数据库时,我都需要添加到原始数字,但我不知道如何操作,如有任何帮助,将不胜感激:C# 不知道如何在数据库中添加值,c#,database,C#,Database,我已经获得了工作输入,但现在每次输入数据库时,我都需要添加到原始数字,但我不知道如何操作,如有任何帮助,将不胜感激: String myConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:/coursework/Databases/runner database.accdb;"; // location of the database OleDbConnection myConnection = new Ole
String myConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:/coursework/Databases/runner database.accdb;"; // location of the database
OleDbConnection myConnection = new OleDbConnection(myConnectionString); // To create the database connection
OleDbCommand myCommand = new OleDbCommand(); // Use the connection for the command
myCommand.Connection = myConnection;
try
{
myConnection.Open(); // Opens the database connection
string query = "insert into tblTrainingInformation ([Username],[Calories Burnt]) values('"+GlobalUsername.username+"','" + this.txtCaloriesBurntRun.Text + "')";
OleDbCommand createCommand = new OleDbCommand(query, myConnection);
createCommand.ExecuteNonQuery();
MessageBox.Show("Your running information has been saved");
myConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
如果要更新现有记录,在字段中添加新值,或插入新记录(如果不存在),则需要知道数据库是否已包含用户名。 因此,您需要先运行SELECT查询,然后决定是需要更新还是插入 Access没有像MySql或Sql Server这样的UPSERT语句
String myConnectionString = ".....";
string querySel = @"SELECT [Username],[Calories Burnt]
FROM tblTrainingInformation
WHERE [Username] = @uname";
using(OleDbConnection myConnection = new OleDbConnection(myConnectionString))
using(OleDbCommand myCommand = new OleDbCommand(querySel, myConnection))
{
myConnection.Open();
myCommand.Parameters.AddWithValue("@uname", GlobalUsername.username);
using(OleDbDataReader reader = myCommand.ExecuteReader())
{
int calories = 0;
string query = "";
if(reader.Read())
{
// The record exists, read the calories and add the new value
// then execute the UPDATE
calories = Convert.ToInt32(reader["Calories Burnt"]);
calories += Convert.ToInt32(this.txtCaloriesBurntRun.Text);
query = @"UPDATE tblTrainingInformation
SET [Calories Burnt] = @cal
WHERE [Username] = @uname";
}
else
{
// Record doesn't exist, INSERT the new data
calories = Convert.ToInt32(this.txtCaloriesBurntRun.Text);
query = @"INSERT INTO tblTrainingInformation
([Calories Burnt],[Username])
VALUES(@cal, @uname)";
}
reader.Close();
myCommand.Parameters.Clear();
myCommand.Parameters.AddWithValue("@cal", calories);
myCommand.Parameters.AddWithValue("@uname", GlobalUsername.username);
myCommand.ExecuteNonQuery();
MessageBox.Show("Your running information has been saved");
}
}
我在这里做了一些假设。
首先,我假设UserName是这个表中的主键,因此您可以使用WHERE on UserName值检索记录。
第二个假设是燃烧的能量场类型。
它应该是一个数字字段,为了简化示例,我认为它是一个整数
如果这些假设不正确,则应对其进行检查和修正
注意使用Using语句来正确处理连接、命令和读取器。从查询中删除字符串连接是另一个重要问题。您应该始终使用参数集合来避免Sql注入,尽管在解析值时不太可能存在访问和错误
关于参数顺序的最后说明。OleDb希望参数以参数占位符在查询文本中显示的确切顺序显示,因此我颠倒了插入顺序以与更新命令兼容此处失败的是什么,消息是什么。您还需要使用命令参数来避免sql注入。原始数字是多少?非常完美。没有任何问题,我已设法使数据库的输入正常工作。我需要能够添加到数据库中原始输入的数字,这样我就可以获得卡路里燃烧总量。因此,如果用户不存在卡路里燃烧总量,您需要为用户添加一个新行,如果用户存在卡路里燃烧总量,则添加到其总卡路里燃烧总量?