最好的方法“更少的时间”使用实体框架插入批量插入,不包括两个相干表的重复记录?-英雄云拓展知识分享
244
2024-01-22
使用C#和OLEDB连接到访问数据库。
我有3个几近相同的查询字符串:
"select * from stock_head where sh_id = 19;""select * from stock_head where sh_lineno = 32059";
"select * from stock_head where sh_ref='#007705';";

在每种情况下,第一个和第2个检索行,并且在每种情况下的字段,最后一个是一个整数,它是一个char字段,并且该行确切存在:
ExecuteQuery - select * from stock_head where sh_lineno = 32059ID 7705, #007705, 32059, NS, 'NO SALE', 04/02/2017 14:29:00
1 row(s) found
ExecuteQuery - select * from stock_head where sh_ref='#007705';
0 row(s) found
经过C#和OLEDB在角色字段上查询是不是有些奇怪?我尝试使用“像”而不是'=',和单引号和双引号来界定该值,但杯水车薪。
using System;using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
namespace OleDbTest
{
class Program
{
static void Main( string[] args )
{
// Create Profile File object
ProcessEJFile EJP = new ProcessEJFile(
"Provider=Microsoft.ACE.OLEDB.12.0;" +
@"Data Source=E:\Users\sallyw\Documents\Bar.accdb;" +
"Persist Security Info=False;");
//// Get last reference from the Stock Header file
//object retVal = EJP.ExecuteScalar(
// @"SELECT max(stock_head.[sh_ref]) FROM stock_head where sh_ref like ""[#]%"";", null);
//string maxRef = retVal.ToString();
//Console.WriteLine( "maxRef = {0}", maxRef );
// Get the actual row
string query =
// @"select * from stock_head where sh_ref = '{0}';";
//"select * from stock_head where sh_id = 19;";
"select * from stock_head where sh_lineno = 32059";
List<StockHead> shlist = EJP.GetStockHead(query, null );
if ( shlist == null )
{
Console.WriteLine( "shlist is null" );
}
else
{
foreach (StockHead sh in shlist )
{
Console.WriteLine( sh.ToString() );
}
Console.WriteLine( "{0} row(s) found", shlist.Count());
}
query =
// @"select * from stock_head where sh_ref = '{0}';";
"select * from stock_head where sh_ref='#007705';";
List<StockHead> shlist1 = EJP.GetStockHead(query, null );
if ( shlist1 == null )
{
Console.WriteLine( "shlist1 is null" );
}
else
{
foreach ( StockHead sh in shlist1 )
{
Console.WriteLine( sh.ToString() );
}
Console.WriteLine( "{0} row(s) found", shlist1.Count() );
}
Console.ReadLine();
}
}
class ProcessEJFile
{
AccessDatabase Accdb = null;
public ProcessEJFile( string connectionString )
{
Accdb = new AccessDatabase( connectionString );
}
public List<StockHead> GetStockHead( string sql, params object[] args )
{
DataTable t;
Accdb.ExecuteQuery( out t, sql, args );
if ( t != null )
{
List<StockHead> shlist = new List<StockHead>();
foreach ( DataRow r in t.Rows )
{
StockHead sh = new StockHead( r);
shlist.Add( sh );
}
return shlist;
}
else
{
return null;
}
}
// Get a single value - MAX, COUNT etc.
public Object ExecuteScalar( string sql, params object[] args )
{
return Accdb.ExecuteScalar( sql, args );
}
}
class AccessDatabase
{
public OleDbConnection conn = new OleDbConnection();
public AccessDatabase( string connection )
{
conn.ConnectionString = connection;
}
public bool OpenDatabase()
{
try
{
conn.Open();
}
catch ( Exception ex )
{
return false;
}
return true;
}
public void CloseDatabase()
{
if ( conn == null )
return;
conn.Close();
}
public void ExecuteQuery( out DataTable dataTable, string sql, params object[] args )
{
dataTable = new DataTable();
string query;
// Simplified version not validating or cleaning arguments in any way
if ( args == null )
{
query = sql;
}
else
{
query = string.Format( sql, args );
}
Console.WriteLine( "\nExecuteQuery - {0}", query );
if ( OpenDatabase() )
{
OleDbCommand command = new OleDbCommand( query, conn );
OleDbDataAdapter adapter = new OleDbDataAdapter( command );
adapter.Fill( dataTable );
}
}
public object ExecuteScalar( string sql, params object[] args )
{
Object returnValue = null;
string query = sql;
if ( OpenDatabase() )
{
OleDbCommand cmd = new OleDbCommand( query, (OleDbConnection)conn);
returnValue = cmd.ExecuteScalar();
}
return returnValue;
}
}
class StockHead
{
public int sh_id;
public string sh_ref;
public int sh_lineno = 0;
public string sh_type;
public string sh_supplier = "";
public DateTime sh_datetime;
public StockHead( DataRow row )
{
this.sh_id = (int)row[ "sh_id" ];
this.sh_ref = (string)row[ "sh_ref" ];
if ( !string.IsNullOrEmpty( row[ "sh_lineno" ].ToString() ) )
{
this.sh_lineno = (int)row[ "sh_lineno" ];
}
this.sh_type = (string)row[ "sh_type" ];
if ( !string.IsNullOrEmpty( row[ "sh_lineno" ].ToString() ) )
{
this.sh_supplier = (string)row[ "sh_supplier" ];
}
this.sh_datetime = (DateTime)row[ "sh_datetime" ];
}
public override string ToString()
{
return String.Format( "ID {0}, {1}, {2}, {3}, '{4}', {5}",
this.sh_id, this.sh_ref, this.sh_lineno, this.sh_type, this.sh_supplier, this.sh_datetime );
}
}
}`
您的查询没有错。这是失败的,由于您每次经过在executequery中调用opendatabase履行查询时尝试打开与数据库的连接。由于数据库已打开并且没有履行查询,因此第2次opendatabase失败。
您正在捕获毛病,但甚么都不做。给出的消息是...
The connection was not closed. The connection's current state is open.
可以经过逐渐阅读您的代码轻松找到这一点。您没有在调试中运行它吗?
您可以将其更改成..
public bool OpenDatabase(){
try
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
return true;
}
如果您堕入毛病,请对此做些事情,除返回false
免责声明:
本网址(www.yingxiongyun.com)发布的材料主要源于独立创作和网友匿名投稿。此处提供的所有信息仅供参考之用。我们致力于提供准确且可信的信息,但不对材料的完整性或真实性作出任何保证。用户应自行验证相关信息的正确性,并对其决策承担全部责任。对于由于信息的错误、不准确或遗漏所造成的任何损失,本网址不承担任何法律责任。本网站所展示的所有内容,如文字、图像、标志、音频、视频、软件和程序等的版权均属于原创作者。如果任何组织或个人认为网站内容可能侵犯其知识产权,或包含不准确之处,请即刻联系我们进行相应处理。
发表评论
暂时没有评论,来抢沙发吧~