In Winform, database operations are not secure. Therefore, you can use Winform to call WebService to perform database operations.
In VS2010, create a Web service program. First, create an empty Web application with its own name. 2. Right-click the project you just created, select add, and select Web Service in the pop-up box. Set your name and click OK to create a Web service program.
After the above steps, we can add methods to perform database operations.
The Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Services;
Using System. Data. SqlClient;
Using System. Data;
Namespace WebServerSQL
{
/// <Summary>
/// Summary of WebServerGetSqlData
/// </Summary>
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
[System. ComponentModel. ToolboxItem (false)]
// To allow ASP. net ajax to call this Web service from a script, cancel the comments to the downstream.
// [System. Web. Script. Services. ScriptService]
Public class WebServerGetSqlData: System. Web. Services. WebService
{
// Define the database connection object
Private SqlConnection con;
[WebMethod]
Public DataSet GetInfo (string strSql)
{
String sqlConnect = "initial catalog = database name; server = server name; uid = sa; pwd = password ";
Con = new SqlConnection (sqlConnect );
Con. Open ();
SqlDataAdapter da = new SqlDataAdapter (strSql, con );
DataSet ds = new DataSet ();
Da. Fill (ds );
Return ds;
}
[WebMethod]
Public bool testConnect ()
{
Try
{
// Database connection, define the connection object and connection string, and open
String sqlConnect = "initial catalog = database name; server = server name; uid = sa; pwd = password ";
Con = new SqlConnection (sqlConnect );
Con. Open ();
Return true;
}
Catch
{
Return false;
}
}
}
}
Next, let's take a look at how to call WebService under Winfrom.
First, add a Web service reference. In VS2010, right-click the project and you will not be able to add a Web reference in the pop-up menu. Is it possible to add a Web reference in Winform? It can be added after verification. We only need to select Web Service reference, and then click advanced in the pop-up dialog box, then you will see a new pop-up dialog box with Web reference added, enter the URL of the WebService and follow the steps. For example:
In Winfrom, we can call the method provided by WebService. The Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Namespace WinformGetWebServerSQL
{
Public partial class FrmWebserver: Form
{
Public FrmWebserver ()
{
InitializeComponent ();
}
// Define the web service object
Localhost. WebServerGetSqlData webData = new localhost. WebServerGetSqlData ();
// Test whether the database connection is successful
Private void btnTest_Click (object sender, EventArgs e)
{
If (webData. testConnect ())
{
Label2.Text = "successful ";
}
Else
{
Label2.Text = "failed ";
}
}
// Display the table data in the database
Private void btnShow_Click (object sender, EventArgs e)
{
String strsql = "select * from item ";
DataSet ds = webData. GetInfo (strsql );
DVShowInfo. DataSource = ds. Tables [0];
}
}
}
Now, a simple webservice calling program is complete. Interested friends can add more methods in WebService, such as adding, deleting, and modifying databases, for winform calling.
I hope you can give me more comments and learn and make progress together!