.net 关于数据库数据检索速度疑问?

emailqjc 2011-10-12 11:45:11
SQL检索代码如下:
select ta.*,tb.BillNo as BillNoCX from (select GoodID,ScanCode as ScanCodID,GoodName,Spec,Unit,FactCode as FactCodID,RetailPric,MemPric,WholeSalePric,STBYPricA,STBYPricB,STBYPricC,STBYPricD,STBYPricE,LogPacks,goodtypeid as GoodType,SpeOffFlag,CXRange,MemberBeginLevel,MemberEndLevel,CXPric,CXKL,GoodPrtyID from jcv_goodbaseinfo where (ScanCode='0101010641' or goodid='0101010641' or goodname like '%0101010641%')) ta left join jcpscxinfo tb on ta.goodid=tb.goodid

在SQL查询分析器里执行耗时:0.1秒
在.NET中利用数据库操作类检索耗时在2~3秒,请问什么原因,我的.NET代码如下:
DateTime dt1 = System.DateTime.Now;

DataTable dtReturn;

string strSearchVar = textBox1.Text.Trim();

string strSql = "select ta.*,tb.BillNo as BillNoCX from (select GoodID,ScanCode as ScanCodID,GoodName,Spec,Unit,FactCode as FactCodID,RetailPric,MemPric,WholeSalePric,STBYPricA,STBYPricB,STBYPricC,STBYPricD,STBYPricE,LogPacks,goodtypeid as GoodType,SpeOffFlag,CXRange,MemberBeginLevel,MemberEndLevel,CXPric,CXKL,GoodPrtyID from jcv_goodbaseinfo where (ScanCode='" + strSearchVar + "' or goodid='" + strSearchVar + "' or goodname like '%" + strSearchVar + "%')) ta left join jcpscxinfo tb on ta.goodid=tb.goodid ";


SqlCommand sqlCommand = new SqlCommand();
sqlCommand.CommandType = CommandType.Text;
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = strSql;

if (sqlConnection.State != ConnectionState.Open)
{
sqlConnection.Open();
}

SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
sqlDataAdapter.SelectCommand = sqlCommand;
DataSet dataSet = new DataSet();

SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
sqlDataAdapter.Fill(dataSet);
dtReturn = dataSet.Tables[0];
double k = (DateTime.Now - dt1).TotalSeconds; //计算耗时

sqlCommand.Dispose();
sqlDataAdapter.Dispose();
//sqlConnection.Close();
...全文
67 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ny-6000 2011-10-12
  • 打赏
  • 举报
回复
数据传输,网站响应.

emailqjc 2011-10-12
  • 打赏
  • 举报
回复
为什么第一次访问查询比较慢,第二次就比较快了
DateTime dt1 = System.DateTime.Now;
string strSearchVar = textBox1.Text.Trim();

string strSql = "select ta.*,tb.BillNo as BillNoCX from (select GoodID,ScanCode as ScanCodID,GoodName,Spec,Unit,FactCode as FactCodID,RetailPric,MemPric,WholeSalePric,STBYPricA,STBYPricB,STBYPricC,STBYPricD,STBYPricE,LogPacks,goodtypeid as GoodType,SpeOffFlag,CXRange,MemberBeginLevel,MemberEndLevel,CXPric,CXKL,GoodPrtyID from jcv_goodbaseinfo where (ScanCode='" + strSearchVar + "' or goodid='" + strSearchVar + "' or goodname like '%" + strSearchVar + "%')) ta left join jcpscxinfo tb on ta.goodid=tb.goodid ";

DataTable dt = mydbacc.ExecuteDataSet(CommandType.Text, strSql).Tables[0];

double k = (DateTime.Now - dt1).TotalSeconds;

label1.Text = "label1 " + k.ToString();//第一次用的时间

dt1 = System.DateTime.Now;
dt = myDbacc.SimpleExecuteDataSet(strSql).Tables[0];

k = (DateTime.Now - dt1).TotalSeconds;

label2.Text = "label2 " + k.ToString();//第二次用的时间
emailqjc 2011-10-12
  • 打赏
  • 举报
回复
我是在WinForm里面哈

17,740

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧