【发布时间】:2018-10-19 16:28:53
【问题描述】:
所以我有一个我正在做的小项目。基本上我想做的是创建另一个类,包括 SQL 连接以及结果。但是,它告诉我:
名称“firstName”不存在。
当我将它放在为 Visual Studio 中的设计器模式创建的页面上时,它会消失并正常工作。
info.cs:
public void GetInfo(string accountNumber)
{
string source = helper.CnnVal("WorkflowConfiguration");
SqlConnection con = new SqlConnection(source);
con.Open();
string sqlSelectQuery = $"Select TOP 1 * From [Workflow Creation].[dbo].[ssFields] Where Field16 =" + int.Parse(accountNumber);
SqlCommand cmd = new SqlCommand(sqlSelectQuery, con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
firstName.Text = (dr["Field1"].ToString());
lastName.Text = (dr["Field2"].ToString());
dateOfbirth.Text = (dr["Field3"].ToString());
socialSecurity.Text = (dr["Field4"].ToString());
}
con.Close();
}
我想引用“设计器”代码页。所以我可以参考下面的btn点击结果:
namespace WindowsFormsApp1
{
public partial class dataBase : Form
{
List<Information> people = new List<Information>();
private personalInfo personal = new personalInfo();
public dataBase()
{
InitializeComponent();
}
public void searchBtn_Click(object sender, EventArgs e)
{
dataAccess db = new dataAccess();
people = db.GetPeople(accountNumber.Text);
ListBoxPeople.DataSource = people;
ListBoxPeople.DisplayMember = "FullInfo";
//Would like to references here from info.cs
}
【问题讨论】:
-
你需要在声明
firstName的地方包含代码。 -
另外,请格式化您的代码 - 更正缩进并删除所有多余的空格
-
@rory.ap 它将在第二组代码中。任何地方都没有 firstName 的声明。我知道,当我将 info.cs 中的代码粘贴到第二组代码的引用位置时,它可以工作。
-
您应该知道您的代码容易受到 SQL 注入攻击。使用参数化查询!
-
不,您需要在此处在您的问题中包含您的代码,以便我们看到它。