WCF到DB Null异常

时间:2019-04-17 14:10:42

标签: wcf

我正在尝试将WPF连接到WCF到DB,但是我得到了空异常 而且我不知道如何调试它或出了什么问题

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace StockWcf
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to 
    change the class name "Service1" in both code and config file together.
    public class Service1 : IService1
    {
        SqlConnection conn;
        SqlCommand comm;

        SqlConnectionStringBuilder connStringBuilder;
        void ConnectToDb()
        {
            connStringBuilder = new SqlConnectionStringBuilder();
            connStringBuilder.DataSource = @"(LocalDB)\MSSQLLocalDB";
            connStringBuilder.InitialCatalog = @"C:\Projects\Assignment 3\StockApp\StockApp\StockApp.mdf";
            connStringBuilder.Encrypt = true;
            connStringBuilder.TrustServerCertificate = true;
            connStringBuilder.ConnectTimeout = 30;
            connStringBuilder.AsynchronousProcessing = true;
            connStringBuilder.MultipleActiveResultSets = true;
            connStringBuilder.IntegratedSecurity = true;

            conn = new SqlConnection(connStringBuilder.ToString());
            comm = conn.CreateCommand();

        }
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }

        public Member GetMember(Member m)
        {
            Member member = new Member();
            try
            {
                comm.CommandText = "select * from Users where username=@username and password=@password";
                comm.Parameters.AddWithValue("username", m.username);
                comm.Parameters.AddWithValue("password", m.password);
                comm.CommandType = CommandType.Text;

                conn.Open();

                SqlDataReader reader = comm.ExecuteReader();
                while (reader.Read())
                {
                    member.Id = Convert.ToInt32(reader[0]);
                    member.username = reader[1].ToString();
                    member.firstname = reader[2].ToString();
                    member.lastname = reader[3].ToString();
                    member.email = reader[4].ToString();
                    member.password = reader[5].ToString();
                    member.lastSearchAll = reader[6].ToString();
                    member.lastSearchHistory = reader[7].ToString();
                    member.lastSearchLive = reader[8].ToString();
                }

                return member;
            }
            catch (Exception)
            {

                throw;
            }

            finally
            {
                if(conn != null)
                {
                    conn.Close();
                }
            }
        }
    }
}

System.NullReferenceException:'对象引用未设置为对象的实例。'

每次我尝试通过在Visual Studio中设置断点进行调试时,无论放置在哪里,它都会使我直接抓住(抛出;),但我不知道出了什么问题,请问您是什么原因,因为我是新来的wcf和我一般没有很多连接数据库的经验

http://prntscr.com/nd8tn0

0 个答案:

没有答案