繁体   English   中英

如何显示从数据库到数据绑定的datagridview的数据?

[英]How to a display the data from database to the databound datagridview?

那么如何显示从数据库到datagridview的数据呢?

我正在使用Visual Studio 2012

我正在使用Window Form Application

这是一些与制作代码有关的代码

using System.Data.SqlServerCe;
SqlCeConnection con = new SqlCeConnection(@"Data Source=C:\Co-op\Contact\Contact\ContactDataBase.sdf;Password=********");

此列在我的桌子上:

IdNameAdressPhoneEmail

这是如何做的一个例子

using (SqlDataAdapter sa = CreateCustomAdapter(OrderIDInt))
        {
            sa.Fill(OrderTable);
            dataGridView1.DataSource = OrderTable;
        }

创建您的SQL数据适配器

 public static SqlDataAdapter CreateCustomAdapter(int OrderIDInt)
    {

        SqlCeConnection con = new SqlCeConnection(@"Data Source=C:\Co-op\Contact\Contact\ContactDataBase.sdf;Password=********");
        con.Open();
        SqlDataAdapter adapter = new SqlDataAdapter();

如果需要,Thes是创建sql参数的好地方

SqlParameter param1 = new SqlParameter("@CURSTAT", record.curstat);

编写选择命令

        adapter.SelectCommand = new SqlCommand("select Id, Name, Adress, Phone, Email from [The name of the table]", coמ);

如果您使用的是sql参数,则应包含thes行

        adapter.SelectCommand.Parameters.Add(param1 );

那你应该退回适配器

return adapter;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM