有没有OleDb的替代品来提高性能?

时间:2012-08-07 17:54:46

标签: c# sql-server winforms

我有一个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快的方法?

2 个答案:

答案 0 :(得分:1)

如果您关注它的表现,请查看SqlBulkCopy

Here's一个例子(在VB.Net中)可以让你了解所涉及的速度增加。

答案 1 :(得分: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();
   }