ASP.NET C#数据绑定
就是用如button实现点击之后,gridview控件绑定并显示SQL数据库的内容,鼠标我会操作,就是建一个sqldatasource控件,然后(我是用存储过程)绑定,实...
就是用如button实现点击之后,gridview控件绑定并显示SQL数据库的内容,鼠标我会操作,就是建一个sqldatasource控件,然后(我是用存储过程)绑定,实现查询在gridview显示,但是代码怎么写?求大神帮我操作!
展开
2个回答
2014-04-25
展开全部
下面这段内容是以前回复其他百度网友的,其中有一个项涉及UI中GridView的绑定(这里没有用到sqldatasource,直接调用三层结构中的BLL层,返回一个DataTable来绑定,如果你用存储过程,你只需要修改一下BLL层调用DAL层访问数据库的方法),可以参考一下:---------------------------------------
第一,
1.设置数据库可以远程访问(以sqlserver为例)
开始菜单---程序--- Microsoft sql server 2005---配置工具--sqlserver 外围配置器
---服务和连接的外围配置器
打开 MSSqlServer---DataBase Engine--远程连接
选择本地连接和远程连接,再选择同时使用TCP/IP 和named pipes 确定
2.创建Winform 应用程序
3.添加应用程序配置文件 App.config 文件,在
<configuration>
</configuration>
之间加入
<connectionStrings>
<add name="dbCon" connectionString="Data Source=127.0.0.1;Initial Catalog=Northwind;User ID=sa;PassWord=sa" providerName="System.Data.SqlClient"/>
</connectionStrings>
其中IP地址和数据库名称、用户名和密码需要改成你本地实际的
第二,
1.数据库建表,
create table mytable(
pkid nvarchar(40) not null,
a nvarchar(40) null
)
insert into mytable(pkid,a) values(newid(),'测试')
2.新建对应此表的实体类
[Serializable]
public class mytable
{
private String _pkid = "";
public String pkid
{
get { return _pkid; }
set { _pkid = value; }
}
private String _a = "";
public String a
{
get { return _a; }
set { _a = value; }
}
}
3.新建操作类:
public class mytableDAO
{
private String _ConnectionString;
public mytableDAO(String ConnectionString)
{
this._ConnectionString = ConnectionString;
}
public int New(Entities.MsgRecord obj)
{
String sql = "insert into mytable(pkid,a) values(@pkid,@a)";
SqlConnection cn = new SqlConnection(this._ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@a", obj.a);
cmd.Parameters.AddWithValue("@pkid",
String.Empty.Equals(obj.pkid) ? System.Guid.NewGuid().ToString() : obj.pkid);
try
{
if (cn.State != ConnectionState.Open)
cn.Open();
return cmd.ExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
public int Update(Entities.mytable obj)
{
String sql = "Update mytable Set a=@a Where pkid=@ObjectID";
SqlConnection cn = new SqlConnection(this._ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@a", obj.a);
cmd.Parameters.AddWithValue("@pkid", obj.pkid);
try
{
if (cn.State != ConnectionState.Open)
cn.Open();
return cmd.ExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
public int Del(Entities.mytable obj)
{
String sql = "delete from mytable Where pkid=@ObjectID";
SqlConnection cn = new SqlConnection(this._ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@ObjectID", obj.pkid);
try
{
if (cn.State != ConnectionState.Open)
cn.Open();
return cmd.ExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
public int Del(String where)
{
String sql = String.Format("delete from mytable Where {0}", where.ToLower().Replace("update", "").Replace("delete", ""));
SqlConnection cn = new SqlConnection(this._ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
try
{
if (cn.State != ConnectionState.Open)
cn.Open();
return cmd.ExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
public DataTable Query(String where)
{
String sql = String.Format("select * from mytable Where {0}", where.ToLower().Replace("update", "").Replace("delete", "").Replace("insert", "").Replace(";", "").Replace("--", "").Replace("exec", ""));
try
{
SqlDataAdapter da = new SqlDataAdapter(sql, new SqlConnection(this._ConnectionString));
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
catch
{
return null;
}
}
}
4.在ui中调用:
4.1 新增:
private void NewData()
{
String conn=System.Configuration.ConfigurationManager.ConnectionStrings["dbCon"].ConnectionString
mytableDao dao=new mytableDao(conn);
mytable obj=new mytable();
obj.a=this.txtbox_a.Text;
obj.b=this.txtbox_b.Text;
obj.c=this.txtbox_c.Text;
dao.New(obj);
}
4.2 取数据绑定GridView
private void LoadData_InitGridView()
{
String conn=System.Configuration.ConfigurationManager.ConnectionStrings["dbCon"].ConnectionString
mytableDao dao=new mytableDao(conn);
Table dt=dao.Query(String.Format(" a ='{0}'",this.txtbox_a.Text));
//gridview
this.GridView1.DataSource=dt;
this.GridView1.DataBind();
//textbox control
this.textbox_a.Text=dt.Rows[0][0].ToString();
}
4.3删除
private void Del()
{
String conn=System.Configuration.ConfigurationManager.ConnectionStrings["dbCon"].ConnectionString
mytableDao dao=new mytableDao(conn);
//根据条件删除指定记录
dao.Del(where);
}
4.4更新
private void Update()
{
String conn=System.Configuration.ConfigurationManager.ConnectionStrings["dbCon"].ConnectionString
mytableDao dao=new mytableDao(conn);
dao.Update(obj);//obj 为mytable的一个实例:需要更新的
}
5.配置文件中连接信息
<connectionStrings>
<add name="dbCon" connectionString="Data Source=127.0.0.1;Initial Catalog=Northwind;User ID=sa;PassWord=sa" providerName="System.Data.SqlClient"/>
</connectionStrings>
6.读取连接信息
System.Configuration.ConfigurationManager.ConnectionStrings["dbCon"].ConnectionString
第一,
1.设置数据库可以远程访问(以sqlserver为例)
开始菜单---程序--- Microsoft sql server 2005---配置工具--sqlserver 外围配置器
---服务和连接的外围配置器
打开 MSSqlServer---DataBase Engine--远程连接
选择本地连接和远程连接,再选择同时使用TCP/IP 和named pipes 确定
2.创建Winform 应用程序
3.添加应用程序配置文件 App.config 文件,在
<configuration>
</configuration>
之间加入
<connectionStrings>
<add name="dbCon" connectionString="Data Source=127.0.0.1;Initial Catalog=Northwind;User ID=sa;PassWord=sa" providerName="System.Data.SqlClient"/>
</connectionStrings>
其中IP地址和数据库名称、用户名和密码需要改成你本地实际的
第二,
1.数据库建表,
create table mytable(
pkid nvarchar(40) not null,
a nvarchar(40) null
)
insert into mytable(pkid,a) values(newid(),'测试')
2.新建对应此表的实体类
[Serializable]
public class mytable
{
private String _pkid = "";
public String pkid
{
get { return _pkid; }
set { _pkid = value; }
}
private String _a = "";
public String a
{
get { return _a; }
set { _a = value; }
}
}
3.新建操作类:
public class mytableDAO
{
private String _ConnectionString;
public mytableDAO(String ConnectionString)
{
this._ConnectionString = ConnectionString;
}
public int New(Entities.MsgRecord obj)
{
String sql = "insert into mytable(pkid,a) values(@pkid,@a)";
SqlConnection cn = new SqlConnection(this._ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@a", obj.a);
cmd.Parameters.AddWithValue("@pkid",
String.Empty.Equals(obj.pkid) ? System.Guid.NewGuid().ToString() : obj.pkid);
try
{
if (cn.State != ConnectionState.Open)
cn.Open();
return cmd.ExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
public int Update(Entities.mytable obj)
{
String sql = "Update mytable Set a=@a Where pkid=@ObjectID";
SqlConnection cn = new SqlConnection(this._ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@a", obj.a);
cmd.Parameters.AddWithValue("@pkid", obj.pkid);
try
{
if (cn.State != ConnectionState.Open)
cn.Open();
return cmd.ExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
public int Del(Entities.mytable obj)
{
String sql = "delete from mytable Where pkid=@ObjectID";
SqlConnection cn = new SqlConnection(this._ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@ObjectID", obj.pkid);
try
{
if (cn.State != ConnectionState.Open)
cn.Open();
return cmd.ExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
public int Del(String where)
{
String sql = String.Format("delete from mytable Where {0}", where.ToLower().Replace("update", "").Replace("delete", ""));
SqlConnection cn = new SqlConnection(this._ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
try
{
if (cn.State != ConnectionState.Open)
cn.Open();
return cmd.ExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
public DataTable Query(String where)
{
String sql = String.Format("select * from mytable Where {0}", where.ToLower().Replace("update", "").Replace("delete", "").Replace("insert", "").Replace(";", "").Replace("--", "").Replace("exec", ""));
try
{
SqlDataAdapter da = new SqlDataAdapter(sql, new SqlConnection(this._ConnectionString));
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
catch
{
return null;
}
}
}
4.在ui中调用:
4.1 新增:
private void NewData()
{
String conn=System.Configuration.ConfigurationManager.ConnectionStrings["dbCon"].ConnectionString
mytableDao dao=new mytableDao(conn);
mytable obj=new mytable();
obj.a=this.txtbox_a.Text;
obj.b=this.txtbox_b.Text;
obj.c=this.txtbox_c.Text;
dao.New(obj);
}
4.2 取数据绑定GridView
private void LoadData_InitGridView()
{
String conn=System.Configuration.ConfigurationManager.ConnectionStrings["dbCon"].ConnectionString
mytableDao dao=new mytableDao(conn);
Table dt=dao.Query(String.Format(" a ='{0}'",this.txtbox_a.Text));
//gridview
this.GridView1.DataSource=dt;
this.GridView1.DataBind();
//textbox control
this.textbox_a.Text=dt.Rows[0][0].ToString();
}
4.3删除
private void Del()
{
String conn=System.Configuration.ConfigurationManager.ConnectionStrings["dbCon"].ConnectionString
mytableDao dao=new mytableDao(conn);
//根据条件删除指定记录
dao.Del(where);
}
4.4更新
private void Update()
{
String conn=System.Configuration.ConfigurationManager.ConnectionStrings["dbCon"].ConnectionString
mytableDao dao=new mytableDao(conn);
dao.Update(obj);//obj 为mytable的一个实例:需要更新的
}
5.配置文件中连接信息
<connectionStrings>
<add name="dbCon" connectionString="Data Source=127.0.0.1;Initial Catalog=Northwind;User ID=sa;PassWord=sa" providerName="System.Data.SqlClient"/>
</connectionStrings>
6.读取连接信息
System.Configuration.ConfigurationManager.ConnectionStrings["dbCon"].ConnectionString
展开全部
你可以后台查询后用DataSet或DataTable接收数据,然后绑定到gridview
用sqldatasource不灵活,很少用的
DataTable dt=--------;
gridview1.DataSourse=dt;
dridview1.DataBind();
通过sql语句获取数据后再绑定
更多追问追答
追问
能不能教一下我,留下联系方式可以么……
能不能教一下我,留下联系方式可以么……
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询