ASP.NET WebService开发指南
通过WebService从数据库中读文件
<!—将连接数据库的字段写成xml中,方便改变数据库 -->
<connectionStrings>
<add name="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>
<compilation debug="true" targetFramework="4.0" />
<webServices>
<protocols>
<add name="HttpPost"/>
<add name="HttpGet"/>
</protocols>
</webServices>
</system.web>
-
添加WebService.asmx,在WebService的方法前面添加[WebMethod]后就可以通过Web客户端调用此方法
[WebMethod]
public string UseLogin(string name, string password)
{
string response = "" ;
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(password))
{
return null;
}
else
{
string sqlmend = "DB_R_Login";// 存储过程的名字
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection_SXDW"].ConnectionString); // 连接数据库的字符串,和前面的Web.config中的<connectionStrings>相对应
connection.Open(); // 打开数据库连接
SqlCommand cm = 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);
SqlDataReader reader = cm.ExecuteReader();
reader.Close();
return role.Value.ToString();// 返回输出参数
}
}
建表
create table System_Users
(
User_ID int identity(1,1) primary key,
User_Type int not null,
User_Password varchar(10) not null,
User_Name varchar(10) not null
)
建立存储过程:
CREATE proc [dbo].[DB_R_Login](@name varchar(64),@password varchar(64),@User_Type numeric(20) output)
as
select @User_Type = user_type from System_Users where User_Name = @name and User_Password = @password;