0
我可能用错误的方法来处理这个问题。当我使用数据访问层技术时,我无法检索数据。如果我在主类使用下面的代码它的工作原理:当我使用数据访问层技术数据集和适配器时,无法检索数据
private void button1_Click(object sender, RoutedEventArgs e)
{
myCon.connectionString();
string SomeString = string.Empty;
SomeString = "SELECT * FROM TableA";
SqlCommand cmd = new SqlCommand(SomeString, myCon.Con);
adapter = new SqlDataAdapter(cmd);
ds = new DataSet();
adapter.Fill(ds, "TableA");
txtID.Text = ds.Tables[0].Rows[rno][0].ToString();
txtFirstName.Text = ds.Tables[0].Rows[rno][1].ToString();
txtLastName.Text = ds.Tables[0].Rows[rno][2].ToString();
}
如果我放在其他类上面的代码我无法检索数据并没有错误。
ClassA的:
public void Button(string id, string firstname, string lastname)
{
myCon.connectionString();
string SomeString = string.Empty;
SqlConnection cnn = myCon.Con;
cnn.Open();
CmdString = "SELECT * FROM TableA";
SqlCommand cmd = new SqlCommand(CmdString, cnn.Con);
adapter = new SqlDataAdapter(cmd);
ds = new DataSet();
adapter.Fill(ds, "Table");
id = ds.Tables[0].Rows[rno][0].ToString();
firstname = ds.Tables[0].Rows[rno][1].ToString();
lastname = ds.Tables[0].Rows[rno][2].ToString();
}
主要类:提前
public partial class MainWindow : Window
{
ClassA objs = new ClassA();
private void button1_Click(object sender, RoutedEventArgs e)
{
objs.Button(txtID.Text, txtFirstName.Text, txtLastName.Text);
}
感谢如果有人能帮助我在这里。
您正在传递字符串的副本作为参数,而不是字符串本身。我还建议使用绑定而不是显式赋值。 – Somedust