⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sinanews.asmx

📁 asp.net经典案例资料
💻 ASMX
字号:
<%@ WebService Language="C#" Class="SinaNews" %>

using System;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;

[WebService(Namespace="http://www.sina.com.cn")]
public class SinaNews
{
  private SqlConnection PrepareConnection()
  {
    string dataSource = "Data Source=localhost;";
    string security = "user id=sa; password=;";
    string initialCatalog = "initial catalog=lesson46;";
    string cnnString = dataSource + security + initialCatalog;
    SqlConnection connection = new SqlConnection(cnnString);
    return connection;
  }

  private bool Authenticate(string user, string password)
  {
    SqlConnection connection = PrepareConnection();
    string strSql = "select [id] from sinauser where username='";
    strSql += user + "' and password='" + password + "'";
    SqlCommand command = new SqlCommand(strSql, connection);

    try
    {
      connection.Open();
      if( command.ExecuteScalar() == null)
        return false;
      else
        return true;
    }
    catch(SqlException e)
    {
      return false;
    } 
    finally
    {
      connection.Close();
    }
  }

  [WebMethod]
  public DataSet GetNews(string user, string password)
  {
    if( Authenticate(user, password) == false)
      return null;

    SqlConnection connection = PrepareConnection();
    SqlDataAdapter adapter = new SqlDataAdapter();
    DataSet dataSet = new DataSet();

    try
    {
      // Get Domestic news
      SqlCommand command;
      command = new SqlCommand("Select * from sinanews where type=1", connection);
      adapter.SelectCommand = command;
      adapter.Fill(dataSet, "Domestic");

      // Get IT news
      command = new SqlCommand("Select * from sinanews where type=2", connection);
      adapter.SelectCommand = command;
      adapter.Fill(dataSet, "Inter");

      // Get Sports news
      command = new SqlCommand("Select * from sinanews where type=3", connection);
      adapter.SelectCommand = command;
      adapter.Fill(dataSet, "Sports");
    }
    catch(SqlException e)
    {
      return null;
    } 
    finally
    {
      connection.Close();
    }
    return dataSet;
  }

  [WebMethod]
  public bool Register(string user, string password)
  {
    SqlConnection connection = PrepareConnection();
    string strSql = "insert into sinauser(user, password) values('";
    strSql += user + "', '" + password + "')";
    SqlCommand command = new SqlCommand(strSql, connection);

    try
    {
      connection.Open();
      command.ExecuteNonQuery();
    }
    catch(SqlException e)
    {
      return false;
    } 
    finally
    {
      connection.Close();
    }
    return true;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -