0

我有一个 ms sql 2012 服务器,我使用这个函数来添加/记录:

static String conString = "Provider=SQLOLEDB;Data Source = " + ecommerceServer + "; Initial Catalog = " + eCommercedbName + "; User Id = " + eCommerceUsername + "; Password = " + eCommercepassword + ";";

    public bool InsertSQL(String query, String Tablename)
    {
        OleDbConnection conn = new OleDbConnection(conString);
        try
        {
            conn.Open();
            OleDbCommand command = new OleDbCommand(query, conn);
            int count = command.ExecuteNonQuery();
            return (count > 0) ? true : false;
        }
        catch
        {
            if (conn.State.ToString() == "Open")
            {
                conn.Close();
            }
        }
        return false;
    }

该功能很慢(例如,我对 MySQL 有类似的执行,它的工作速度要快得多。有没有比这更快的方法OleDb

4

2 回答 2

1

如果您担心它的性能,请查看SqlBulkCopy

这是一个示例(尽管在 VB.Net 中),它可以让您了解所涉及的速度增加。

于 2012-08-07T18:27:43.440 回答
0

下面的 Andrew 是一个示例,说明您如何使用 SQL 客户端以及在查询中执行事务。您可以将其用作指南/学习示例,也可以替换变量以适合您当前的示例。还要注意我是如何从 app.config 文件中读取连接字符串的

 private void SomeMethod()
 {
    SqlCommand sqlcmd = new SqlCommand();
    using (SqlConnection sqlConn = new SqlConnection(ConfigurationSettings.AppSettings["strConnectionString"]))//strConnectionString
    {
       sqlConn.Open();
       sqlcmd.Connection = sqlConn;
       SqlTransaction transaction;
       // Start a local transaction.
       transaction = sqlConn.BeginTransaction("DeleteRecs");
       sqlcmd.Transaction = transaction;
       sqlcmd.CommandTimeout = 60;
       sqlcmd.CommandText = "uf_DeleteWeeks";
       sqlcmd.CommandType = System.Data.CommandType.StoredProcedure;
       try
       {
          sqlcmd.ExecuteNonQuery();
          sqlcmd.Transaction.Commit();
       }
       catch (SqlException sqle)
       {

        try
        {
           transaction.Rollback("DeleteRecs");
        }
        catch (Exception ex)
        {

        }
       Console.WriteLine(sqle.Message);
       }
      }
      ((IDisposable)sqlcmd).Dispose();
   }
于 2012-08-07T18:14:16.663 回答