C#中,在Form中建立个DataGridView,怎么样显示SQL数据库里面的表,请详细指导
我是新手,在自学C#,想请教各位高手:C#中,在Form中建立个DataGridView,怎么样显示SQL数据库里面的表,请详细指导:1.我想利用SqlConnectio...
我是新手,在自学C#,想请教各位高手:
C#中,在Form中建立个DataGridView,怎么样显示SQL数据库里面的表,请详细指导:
1.我想利用SqlConnection、SqlCommand、SqlDataAdapter和DataSet实现表内容在GridView中显示。
2.要有详细的代码哦。 展开
C#中,在Form中建立个DataGridView,怎么样显示SQL数据库里面的表,请详细指导:
1.我想利用SqlConnection、SqlCommand、SqlDataAdapter和DataSet实现表内容在GridView中显示。
2.要有详细的代码哦。 展开
7个回答
展开全部
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=(local);uid=sa;pwd=sa;database=DatabaseName";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from DatabaseName";
cmd.Connection = conn;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
conn.Open();
DataSet ds = new DataSet();
sda.Fill(ds,DatabaseName);
conn.Close();
this.myDataGrid.DataSource = ds;
this.DataBind();
}
}
对着书看看每句代码的意思
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=(local);uid=sa;pwd=sa;database=DatabaseName";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from DatabaseName";
cmd.Connection = conn;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
conn.Open();
DataSet ds = new DataSet();
sda.Fill(ds,DatabaseName);
conn.Close();
this.myDataGrid.DataSource = ds;
this.DataBind();
}
}
对着书看看每句代码的意思
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=(local);uid=sa;pwd=sa;database=DatabaseName";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from DatabaseName";
cmd.Connection = conn;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
conn.Open();
DataSet ds = new DataSet();
sda.Fill(ds,DatabaseName);
conn.Close();
this.myDataGrid.DataSource = ds;
this.DataBind();
}
}
就用这个吧,你先连上数据库,在看看效果,以后尝试的写的更规范点
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=(local);uid=sa;pwd=sa;database=DatabaseName";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from DatabaseName";
cmd.Connection = conn;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
conn.Open();
DataSet ds = new DataSet();
sda.Fill(ds,DatabaseName);
conn.Close();
this.myDataGrid.DataSource = ds;
this.DataBind();
}
}
就用这个吧,你先连上数据库,在看看效果,以后尝试的写的更规范点
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SqlConnection conn = new SqlConnection("server=(local);uid=sa;pwd=sa;database=DatabaseName);
SqlCommand cmd = new SqlCommand("select * from DatabaseName",conn);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
conn.Open();
DataSet ds = new DataSet();
sda.Fill(ds);
this.DataGridView1.DataSource = ds.tables[0];
conn.Close();
SqlCommand cmd = new SqlCommand("select * from DatabaseName",conn);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
conn.Open();
DataSet ds = new DataSet();
sda.Fill(ds);
this.DataGridView1.DataSource = ds.tables[0];
conn.Close();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
DataGridView.datasourse = 方法名();
DataGridView.databind();
方法负责查询表里内容,下面是方法
SqlDataAdapter catDA = new SqlDataAdapter("SELECT CategoryID, CategoryName FROM Categories", nwindConn);
DataTable t = new DataTable();
catDA .Fill(t);
return t;
DataGridView.databind();
方法负责查询表里内容,下面是方法
SqlDataAdapter catDA = new SqlDataAdapter("SELECT CategoryID, CategoryName FROM Categories", nwindConn);
DataTable t = new DataTable();
catDA .Fill(t);
return t;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
DataGridView1.datasouse = sqlcommand.ExecuteNonQuery();
DataGridView1.datebind();
这样就可以了....
DataGridView1.datebind();
这样就可以了....
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询