简体   繁体   English

将数据插入数据库C#中的表中

[英]Insert data into a table in database C#

I intend to populate an access DB table that has three columns; 我打算填充一个具有三列的访问数据库表。 Entity(text type), Date and Value(double type). 实体(文本类型),日期和值(双精度类型)。 I wrote the following code by going through some online links. 我通过一些在线链接编写了以下代码。 Although the code runs fine, the table has no data. 尽管代码运行良好,但该表没有数据。 I am probably missing some part. 我可能缺少了一部分。 Any advice? 有什么建议吗?

for (int i = 0; i < model.CDFResults.Count; i++)
{ // connection details to the DB here...
    for (int j = 0; j < model.CDFResults[i].DataPoints.Count; j++)
    {
        OleDbCommand myAccessCommand = new OleDbCommand();
        myAccessCommand.CommandType = CommandType.Text;
        myAccessCommand.CommandText = "INSERT INTO TypeCurves([Entity],[Date],[Value])VALUES(?,?,?)";
        myAccessCommand.Parameters.AddWithValue("@Entity", model.CDFResults[i].catname_db);
        myAccessCommand.Parameters.AddWithValue("@Date", model.CDFResults[i].DataPoints[j].dt);
        myAccessCommand.Parameters.AddWithValue("@Value", model.CDFResults[i].DataPoints[j].CDFVal);


    } // end of FOR(j) loop

} // end of FOR(i) loop

EDIT: Still not working 编辑:仍然无法正常工作

for (int i = 0; i < model.CDFResults.Count; i++)
{ // connection details to the DB here...
    for (int j = 0; j < model.CDFResults[i].DataPoints.Count; j++)
    {

        OleDbConnection thisConnection = new OleDbConnection(connectionname);
        thisConnection.Open();

        OleDbCommand myAccessCommand = new OleDbCommand();
        myAccessCommand.CommandType = CommandType.Text;
        myAccessCommand.CommandText = "INSERT INTO TypeCurves([Entity],[Date],[Value])VALUES(?,?,?)";
        myAccessCommand.Parameters.AddWithValue("@Entity", model.CDFResults[i].catname_db);
        myAccessCommand.Parameters.AddWithValue("@Date", model.CDFResults[i].DataPoints[j].dt);
        myAccessCommand.Parameters.AddWithValue("@Value", model.CDFResults[i].DataPoints[j].CDFVal);

        myAccessCommand.ExecuteNonQuery();    
    } // end of FOR(j) loop
} // end of FOR(i) loop

You need create the connection to the database and execute the query. 您需要创建与数据库的连接并执行查询。

using (OleDbConnection connection = new OleDbConnection(connectionString))
{   
    string query = "INSERT INTO TypeCurves([Entity],[Date],[Value])VALUES(@Entity,@Date,@Value)";
    OleDbCommand myAccessCommand = new OleDbCommand(query, connection);
    myAccessCommand.Parameters.AddWithValue("@Entity", model.CDFResults[i].catname_db);
    myAccessCommand.Parameters.AddWithValue("@Date", model.CDFResults[i].DataPoints[j].dt);
    myAccessCommand.Parameters.AddWithValue("@Value", model.CDFResults[i].DataPoints[j].CDFVal);

    connection.Open();
    myAccessCommand.ExecuteNonQuery();
}

connectionString is whatever your connection string is to your database. connectionString是您与数据库的连接字符串。

In this example you don't need to explicitly close the connection after the query is executed as the connection is wrapped in a using block, and so will be disposed of once it has exited the block. 在此示例中,您无需在执行查询后显式关闭连接,因为连接被包装在using块中,因此一旦退出该块将被丢弃。

https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.executenonquery(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.executenonquery(v=vs.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM