我试图用我的数据库的第一个表填充listView1
。但它不起作用,有人可以帮助我吗?
函数LoadList
无法识别DataTable DT
以下是我正在使用的代码
namespace Project.Data_Layer
{
class Leerling
{
public static DataTable ExecSelect(string Leerling)
{
DataSet DS = new DataSet();
SqlDataAdapter DA;
DA = new SqlDataAdapter(Leerling, SQLString.Conn);
DS.Clear();
DA.Fill(DS);
// 1e tabel uit database
DataTable DT = DS.Tables[0];
return DT;
}
public static bool ExecOverig(string Leerling)
{
bool gelukt = true;
SqlConnection Connect = new SqlConnection(SQLString.Conn);
SqlCommand Cmd = new SqlCommand(Leerling, Connect);
try
{
Cmd.Connection.Open();
Cmd.ExecuteNonQuery();
Connect.Close();
}
catch
{
gelukt = false;
}
return gelukt;
}
public void LoadList()
{
if(DT.tables.count > 0)
{
listView1.DataSource = DT;
listView1.DataBind();
}
else
{
//
}
}
}
}
答案 0 :(得分:0)
在方法' ExecSelect'
之外声明DataTable变量DTDataTable DT = new DataTable();
public static DataTable ExecSelect(string Leerling)
{
DataSet DS = new DataSet();
SqlDataAdapter DA;
DA = new SqlDataAdapter(Leerling, SQLString.Conn);
DS.Clear();
DA.Fill(DS);
// 1e tabel uit database
DT = DS.Tables[0];
return DT;
}
答案 1 :(得分:0)
[enter image description here][1]
string sel = "select * from EmployeeMaster";
DataTable dt = new DataTable();
dt = con.filldata(sel);
foreach(DataRow r in dt.Rows)
{
listView1.Items.Add(r["E_Name"].ToString());
listView1.Items.Add(r["Salary"].ToString());
}
[1]: https://i.stack.imgur.com/72fmI.png