0
我的第一個表是Clinics
的列:如何將對象讀入和寫入SQL Server數據庫?
ClinicId, ClinicName, ClinicShortName
我的第二個表是Employees
的列:
EmployeeId, EmployeeName, ClinicId, OnJob
我也有定義爲一個視圖v_employees
:
SELECT
Employees.EmployeeId, Employees.EmployeeName,
Clinics.ClinicShortName,
Employees.OnJob
FROM
Clinics
INNER JOIN
Employees ON Clinics.ClinicId = Employees.ClinicId
和我C#代碼是:
class Employee
{
public int EmployeeId { get; set; }
public string EmpolyeeName { get; set; }
public int ClinicId { get; set; }
public bool OnJob { get; set; }
}
class Clinic
{
public int ClinicId { get; set; }
public string ClinicName { get; set; }
public string ClinicShortName { get; set; }
}
List<Employee> employeeList = new List<Employee>();
private void InitEmployees()
{
SqlConnection con = new SqlConnection(......);
SqlCommand cmd = new SqlCommand("select * from v_employees", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Employee employee = new Employee()
{
EmployeeId = Convert.ToInt32(dr["EmployeeId"]),
EmpolyeeName = dr["EmployeeName"].ToString(),
// how to use ClinicShortName field in this position?
// Because I want the data to be displayed into DataGridView control.
OnJob = Convert.ToBoolean(dr["OnJob"])
};
employeeList.Add(employee);
}
dr.Close();
con.Close();
dataGridView1.DataSource = employeeList;
}
如何將對象導入SQL Server?
我應該使用List<T>
方法嗎?
非常感謝!我想用另一種方法。 – Vincent