C# SOAPWeb服务在处理所有数据之前完成
我有一个soapweb服务,我正在我的开发环境中进行本地测试。我有一个测试Winforms应用程序,它在本地主机上使用此web服务 当调用web服务时,它意味着从MSSQL 2008 DB中的表中读取数据并返回一个数据集(此部分正在工作) 我的Winforms应用程序遍历数据集的行,并将数据集的行插入本地数据库。基本上,我是通过web服务将数据从一个数据库的一个表复制到另一个数据库的表(1:1) 问题是。。 …我通过web服务获得了X千行,当我在Winforms应用程序上迭代这些行时,代码在200-300行左右停止循环。我卡住了一个断点,并使用VisualStudio的“即时窗口”验证了有X千行包含针对它们的数据 该项目是一个旧的.NET 2.0网站,它使用旧版本的Microsoft Enterprise Library v2.0.50727进行数据访问 以下是Winforms应用程序上有问题的代码:C# SOAPWeb服务在处理所有数据之前完成,c#,asp.net,.net,web-services,enterprise-library,C#,Asp.net,.net,Web Services,Enterprise Library,我有一个soapweb服务,我正在我的开发环境中进行本地测试。我有一个测试Winforms应用程序,它在本地主机上使用此web服务 当调用web服务时,它意味着从MSSQL 2008 DB中的表中读取数据并返回一个数据集(此部分正在工作) 我的Winforms应用程序遍历数据集的行,并将数据集的行插入本地数据库。基本上,我是通过web服务将数据从一个数据库的一个表复制到另一个数据库的表(1:1) 问题是。。 …我通过web服务获得了X千行,当我在Winforms应用程序上迭代这些行时,代码在20
public static int PopulateLocalTable(DataSet ds, string DBInstance, string DBServer)
{
if (ds.Tables.Count == 0)
{
return 0;
}
else if (ds.Tables[0].Rows.Count == 0)
{
return 0;
}
else
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
using (var conn = new SqlConnection(string.Format("Data Source={0}; Initial Catalog={1}; Integrated Security=True;", DBInstance, DBServer)))
{
conn.Open();
using (var command = new SqlCommand("dbo.myStoredProc", conn))
{
command.CommandType = CommandType.StoredProcedure;
//Added for debugging purposes.
int id = Convert.ToInt32(dr["ID"]);
int teamid = Convert.ToInt32(dr["TeamID"]);
int companyid = Convert.ToInt32(dr["CompanyID"]);
string team_name = Convert.ToString(dr["Team_Name"]);
DateTime date = Convert.ToDateTime(dr["Date"]);
string aaa = Convert.ToString(dr["aaa"]);
int bbb = Convert.ToInt32(dr["bbb"]);
decimal ccc = Convert.ToDecimal(dr["ccc"]);
DateTime ddd = Convert.ToDateTime(dr["ddd"]);
command.Parameters.Add(new SqlParameter("@ID", id));
command.Parameters.Add(new SqlParameter("@TeamID", teamid));
command.Parameters.Add(new SqlParameter("@CompanyID", companyid));
command.Parameters.Add(new SqlParameter("@Team_Name", team_name));
command.Parameters.Add(new SqlParameter("@Date", date));
command.Parameters.Add(new SqlParameter("@aaa", aaa));
command.Parameters.Add(new SqlParameter("@bbb", bbb));
command.Parameters.Add(new SqlParameter("@ccc", ccc));
command.Parameters.Add(new SqlParameter("@ddd", ddd));
command.ExecuteNonQuery();
}
}
}
return ds.Tables[0].Rows.Count;
}
}
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="Service1Soap" closeTimeout="01:00:00" openTimeout="01:00:00" receiveTimeout="01:00:00" sendTimeout="01:00:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="6553600" maxBufferPoolSize="524288" maxReceivedMessageSize="6553600" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
<message clientCredentialType="UserName" algorithmSuite="Default"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:11288/Service1.asmx" binding="basicHttpBinding" bindingConfiguration="Service1Soap" contract="ServiceReference1.Service1Soap" name="Service1Soap"/>
</client>
</system.serviceModel>
以下是Winforms应用程序上的我的app.config设置:
public static int PopulateLocalTable(DataSet ds, string DBInstance, string DBServer)
{
if (ds.Tables.Count == 0)
{
return 0;
}
else if (ds.Tables[0].Rows.Count == 0)
{
return 0;
}
else
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
using (var conn = new SqlConnection(string.Format("Data Source={0}; Initial Catalog={1}; Integrated Security=True;", DBInstance, DBServer)))
{
conn.Open();
using (var command = new SqlCommand("dbo.myStoredProc", conn))
{
command.CommandType = CommandType.StoredProcedure;
//Added for debugging purposes.
int id = Convert.ToInt32(dr["ID"]);
int teamid = Convert.ToInt32(dr["TeamID"]);
int companyid = Convert.ToInt32(dr["CompanyID"]);
string team_name = Convert.ToString(dr["Team_Name"]);
DateTime date = Convert.ToDateTime(dr["Date"]);
string aaa = Convert.ToString(dr["aaa"]);
int bbb = Convert.ToInt32(dr["bbb"]);
decimal ccc = Convert.ToDecimal(dr["ccc"]);
DateTime ddd = Convert.ToDateTime(dr["ddd"]);
command.Parameters.Add(new SqlParameter("@ID", id));
command.Parameters.Add(new SqlParameter("@TeamID", teamid));
command.Parameters.Add(new SqlParameter("@CompanyID", companyid));
command.Parameters.Add(new SqlParameter("@Team_Name", team_name));
command.Parameters.Add(new SqlParameter("@Date", date));
command.Parameters.Add(new SqlParameter("@aaa", aaa));
command.Parameters.Add(new SqlParameter("@bbb", bbb));
command.Parameters.Add(new SqlParameter("@ccc", ccc));
command.Parameters.Add(new SqlParameter("@ddd", ddd));
command.ExecuteNonQuery();
}
}
}
return ds.Tables[0].Rows.Count;
}
}
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="Service1Soap" closeTimeout="01:00:00" openTimeout="01:00:00" receiveTimeout="01:00:00" sendTimeout="01:00:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="6553600" maxBufferPoolSize="524288" maxReceivedMessageSize="6553600" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
<message clientCredentialType="UserName" algorithmSuite="Default"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:11288/Service1.asmx" binding="basicHttpBinding" bindingConfiguration="Service1Soap" contract="ServiceReference1.Service1Soap" name="Service1Soap"/>
</client>
</system.serviceModel>
如果我试图修改连接/命令超时,但无法使其正常工作。这就像连接超时或流受到限制一样。我希望这只是一个配置设置。。提前谢谢 您的问题似乎是在循环之外有
命令
创建,因此最终会添加越来越多的参数
在循环内创建一个新的命令
对象
您可以使用using
语句来确保连接和命令被清除:
using(var connection = new SqlConnection("..."))
{
connection.Open();
using(var command = connection.CreateCommand())
{
// do stuf
}
}
更新:我现在注意到参数已清除。无论如何,请尝试在循环中创建命令:)检查web方法的BufferResponse属性。默认值为TRUE(响应已缓冲),但对于大量数据,不建议缓冲:请尝试将其设置为FALSE
请参阅。ugh,事实证明我在调用的存储过程中有一个逻辑问题。我将此标记为答案,因为它使用Using语句,根据我所知,该语句以更安全的方式处理SQL连接/命令。