ASP.NET WebService开发指南

来源:互联网 发布:win10默认网卡mac地址 编辑:程序博客网 时间:2024/05/17 20:28

ASP.NET WebService开发指南

         通过WebService从数据库中读文件

  •  在web.config加上如下字段

<!—将连接数据库的字段写成xml中,方便改变数据库-->

<connectionStrings>

    <addname="Connection_SXDW"connectionString="Data Source=.\;Initial Catalog=SXDW;Integrated Security=True;User ID=sa;Password=cxx123456;"providerName="System.Data.SqlClient"/>

  </connectionStrings>

<!—添加如下字段,是提示请求的方式,可以是POST,也可以是GET-->

<system.web>

      <compilationdebug="true"targetFramework="4.0"/>

      <webServices>

        <protocols>

          <addname="HttpPost"/>

<addname="HttpGet"/>

        </protocols>

      </webServices>

</system.web>

  •  添加WebService.asmx,在WebService的方法前面添加[WebMethod]后就可以通过Web客户端调用此方法

        [WebMethod]

        publicstring UseLogin(string name, string password)

        {

           string response = "" ;

           if (string.IsNullOrEmpty(name) ||string.IsNullOrEmpty(password))

            {

               return null;

            }

           else

            {

               

             stringsqlmend = "DB_R_Login";//存储过程的名字

            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection_SXDW"].ConnectionString);// 连接数据库的字符串,和前面的Web.config中的<connectionStrings>相对应

            connection.Open(); // 打开数据库连接

            SqlCommandcm = new SqlCommand(sqlcommend, connection);// 执行sql语句或存储过程

            cm.CommandType =CommandType.StoredProcedure; ;//执行存储过程

 

           //存储过程的输入参数

cm. Parameters.Add("@name",SqlDbType.VarChar, 64).Value = name;

            cm.Parameters.Add("@password",SqlDbType.VarChar, 64).Value = password;

 

           // 存储工程的输出参数

           SqlParameter role = new SqlParameter("@User_Type",SqlDbType.VarChar, 20);

            role.Direction =ParameterDirection.Output;

            cm.Parameters.Add(role);

            SqlDataReaderreader = cm.ExecuteReader();

            reader.Close();

           return role.Value.ToString();//返回输出参数

              }

         }

  •   数据库表的建立和存储过程的创建

     建表

      createtable System_Users

    (

       User_ID int identity(1,1)primary key,

       User_Typeint not null,

       User_Passwordvarchar(10)not null,

       User_Namevarchar(10)not null

     )

     建立存储过程:

      CREATEproc [dbo].[DB_R_Login](@namevarchar(64),@passwordvarchar(64),@User_Typenumeric(20)output)

    as

       select@User_Type = user_type from System_Userswhere User_Name= @name and User_Password = @password;

 

原创粉丝点击