首页 > 解决方案 > WCF 到 DB 空异常

问题描述

我正在尝试将 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

标签: wcf

解决方案


推荐阅读