我是 ASP.Net webservices 的完整初学者,谁能给我指出一个很好的教程,通过它我可以实现一个带有 SQL Server 数据库连接的 web 服务?
提前致谢
我是 ASP.Net webservices 的完整初学者,谁能给我指出一个很好的教程,通过它我可以实现一个带有 SQL Server 数据库连接的 web 服务?
提前致谢
转到   Visual Studio>New Project(选择 .Net Framework 3.5)> ASP.net Web Service Application
这将创建一个 Web 服务,其HelloWorld示例如下
    public string HelloWorld()
    {
        return "Hello World";
    }
要创建客户端可以通过网络访问的新方法,请在[WebMethod]标签下创建函数。
添加 using 语句,例如
using System.Data;
using System.Data.SqlClient;
创建一个SqlConnection喜欢
SqlConnection con = new SqlConnection(@"<your connection string>");
创造你的SqlCommand喜欢
 SqlCommand cmd = new SqlCommand(@"<Your SQL Query>", con);
通过调用打开连接
con.Open();
try-catch在如下块中执行查询:
try
        {
            int i=cmd.ExecuteNonQuery();
            con.Close();
        }
        catch (Exception e)
        {
            con.Close();
            return "Failed";
        }
记住ExecuteNonQuery()不返回游标它只返回受影响的行数,对于select需要数据读取器的操作,使用SqlDataReader  类似
SqlDataReader dr = cmd.ExecuteReader();
并像使用阅读器一样
using (dr)
            {
                while (dr.Read())
                {
                    result = dr[0].ToString();
                }
                dr.Close();
                con.Close();
            }
    这是一个视频,将引导您了解如何在 ASP.NET Web 服务中从 MS SQL Server 检索数据。