0

嗨,我试图将 Windows 中的点从数据库更新到数据库,但我不确定如何从变量“totalPoints”获取信息,并将其插入数据库的“点”字段中

using (OleDbConnection conn = new OleDbConnection(strCon))
        {
            String sqlPoints = "UPDATE points FROM customer WHERE [customerID]="
            + txtCustomerID.Text;
            conn.Open();


            conn.Close();
        }

谢谢你的帮助!

4

1 回答 1

3

首先,您应该使用参数化查询——这很容易受到 SQL 注入的影响。

看看这里:参数化查询如何帮助防止 SQL 注入?

要回答您的问题,您需要研究OleDbCommandExecuteNonQuery

public void InsertRow(string connectionString, string insertSQL)
{
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        // The insertSQL string contains a SQL statement that
        // inserts a new row in the source table.
        OleDbCommand command = new OleDbCommand(insertSQL);

        // Set the Connection to the new OleDbConnection.
        command.Connection = connection;

        // Open the connection and execute the insert command.
        try
        {
            connection.Open();
            command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        // The connection is automatically closed when the
        // code exits the using block.
    }
}

http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnection(v=vs.100).aspx

此外,您可能需要重新查看您的 SQL - 不确定您要完成什么。如果您使用的是 SQL Server,则语法应类似于UPDATE TABLE SET FIELD = VALUE WHERE FIELD = VALUE.

祝你好运。

于 2013-01-12T22:33:54.147 回答