I have a SOAP web-service I am testing locally in my development environment. I have a test Winforms application that consumes this web-service on localhost.
When the web-service is called it is meant to read from a table in a MSSQL 2008 DB and return a dataset (this part is working).
My Winforms application iterates through the rows of the dataset and inserts the dataset's rows into the local DB. Basically, I am copying data from one table on one database to another database's table (1:1) via web-service.
..is that I am getting X thousand rows via the web-service and when I iterate through them on my Winforms application the code just stops looping at around 200-300 rows. I have stuck a breakpoint and using Visual Studio's 'Immediate Window' I have verified that there are X thousand rows with data against them.
The project is an old .NET 2.0 Web site, it uses an old version of the Microsoft Enterprise Library v2.0.50727 for data-access.
Here is the problematic code on the Winforms application:
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;
}
}
Here is my app.config settings on the Winforms application:
<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>
If I have tried modifying the connection/command timeout and I can not get it working. It is like the connection is timing out or the stream is being restricted. I am hoping it is just a config setting.. Thanks in advance.
Your problem appears to be that you have the command
creation outside the loop so you end up adding more and more parameters.
Create a new command
object inside the loop.
You can use the using
statement to ensure that connections and commands are cleaned up:
using(var connection = new SqlConnection("..."))
{
connection.Open();
using(var command = connection.CreateCommand())
{
// do stuf
}
}
Update : I noticed now that the parameters are cleared. Try creating the command in the loop anyway :)
Check the BufferResponse property of the web method. The default value is TRUE (the response is buffered), but for large amount of data, buffering is not suggested: try set it to FALSE.
See msdn .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.