Monday, March 26, 2012

How to create internet connection?

Hi everybody,
Is there a client side approach of sending and receiving data from a sql database without a web service or a server component (servicedcomponent and server app)? How can you simulate a network connection over the internet that could send and receive data from and to database?

How can you create a class that would do the same task as a network connection using internet communication as medium? Please point me to topics and documents about this matter.

Thanks.

denpsia

SQLServer provides this feature.

I move this post.

|||

In .NET you would use the HttpWebRequest class, here is a simple bit of sample code to send a SOAP packet.

private void soapRequest()
{
Uri soapUri = new Uri("http://" + soapServer + "/clear_integrated");
string soapRequest =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
"xmlns:sql=\"http://schemas.microsoft.com/sqlserver/2004/SOAP\">" +
"<SOAP-ENV:Body>" +
"<sql:sqlbatch>" +
"<sql:BatchCommands>" +
"select * from authors" +
"</sql:BatchCommands>" +
"</sql:sqlbatch>" +
"</SOAP-ENV:Body>" +
"</SOAP-ENV:Envelope>";

System.Text.UTF8Encoding utfEncoder = new UTF8Encoding();
byte [] requestBytes = utfEncoder.GetBytes(soapRequest);

// Do the work.
for(int i=1; i<=loopCount; i++)
{
try
{
HttpWebRequest webRequest = (System.Net.HttpWebRequest)WebRequest.Create(soapUri);
webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
webRequest.Timeout = timeout;
webRequest.KeepAlive = true;
webRequest.Headers.Add("SOAPAction", "\"http://schemas.microsoft.com/sqlserver/2004/SOAPsqlbatch\"");
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
webRequest.ContentLength = requestBytes.Length;
Stream requestStream = webRequest.GetRequestStream();
requestStream.Write(requestBytes, 0, requestBytes.Length);
requestStream.Close();
WebResponse webResponse = webRequest.GetResponse();

StreamReader responseStream =
new System.IO.StreamReader(webResponse.GetResponseStream(), Encoding.UTF8);

string stringResponse = responseStream.ReadToEnd();
responseStream.Close();
webResponse.Close();

if (1445 != stringResponse.Length) failCount++;

}
catch (Exception ex)
{
logex(ex);

}
}
}

|||

Hi Matt,

This topic is quite old. Thanks for the reply, at that time I post this topic, I am working on windows application on a tablet pc that needs internet connection and remote access to a remote MySQL database. This code of yours is for asp.net and how exactly would the data be transmitted? What protocol or utility would handle the actual sending of data, in my work then we uses VPN to establish remote database connection and ras dialup to establish internet connection. Thanks for the code, it might proved useful next time.

den2005

sql

No comments:

Post a Comment