wcf - 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 的新手,我一般没有太多连接到数据库的经验
解决方案
推荐阅读
- python - 为什么使用按键功能这么慢?
- javascript - 无法获取显示交互式内容的按钮
- node.js - VSCode:无法在初始启动模块之外设置断点
- c++ - 将数组分成更小的连续部分,使 NEO 值最大
- vba - 宏:从两个工作表中复制数据并在第三个中加入
- acumatica - 停止将计费类型/条款附加到合同发票的交易详细信息
- android - 使用充气机和放气机压缩超过一次
- hl7-fhir - FHIR:ImagingStudy 注释
- webpack - 在 vue 的 simple-webpack 模板中,build.js 来自哪里?
- java - 从桌面快捷方式运行 bash 脚本时保持终端打开