将Web服务连接到SQL Server

时间:2014-09-24 12:36:50

标签: sql asp.net web-services

net和im制作用户注册然后登录的Web服务。我已经建立了一个数据库。其中" ID","用户名","密码" 。在webserive中,它显示哪个用户登录 。此代码不起作用(name = reader [0] .ToString(); return name;)name是红线。 plz解释或纠正错误哪里错误

这是我的连接字符串

<connectionStrings>
      <add connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Abdul Samad\Documents\Visual Studio 2013\Projects\WebApplication8\WebApplication8\App_Data\webserver_database.mdf;Integrated Security=True" name="webconnectionstr"/>
</connectionStrings>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace WebApplication8
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        public int Register(string name , string password)
        {
            SqlConnection connection = new SqlConnection();
            try
            {
                connection.ConnectionString = ConfigurationManager.ConnectionStrings["webconnectionstr"].ToString();
                connection.Open();
                SqlCommand cmd = new SqlCommand(@"insert into [userTable] (username,password) values
                    ('" + name + "','" + password + "')", connection);
                cmd.ExecuteNonQuery();
                return 1;
            }

            catch(Exception ex) {
                return 0;
            }
            finally 
            { 
                connection.Close();
            }

        }
        [WebMethod]
        public int getUsername(int id)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["webconnectionstr"].ToString();
            con.Open();
            SqlCommand cmd = new SqlCommand(@"select username from [userTable] where userId='" + id + "'", con);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
               name = reader[0].ToString();
            }
            return cmd;
            con.Close();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您应该返回一个字符串(假设您正在尝试从表中返回该名称。)

   [WebMethod]
        public string getUsername(int id)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["webconnectionstr"].ToString();
            con.Open();
            SqlCommand cmd = new SqlCommand(@"select username from [userTable] where userId='" + id + "'", con);
            SqlDataReader reader = cmd.ExecuteReader();
string str = "";
            while (reader.Read())
            {
               str = reader[0].ToString();
            }
            con.Close();
            return str;

        }

编辑:更好的选项,因为您只需要一个值。

string str = cmd.ExecuteScalar().ToString();
相关问题