public class fangfa
{
//
//TODO: 在此处添加构造函数逻辑
//
SqlConnection conn = null;//连接数据库的对象
//构造函数链接数据库 下
public fangfa()
{
if(conn==null)
{
conn=new SqlConnection();
conn.ConnectionString=@"Data Source=N\SQLEXPRESS;Initial Catalog=feiteng;Integrated Security=True";//连接数据库的字符串
}
if(conn.State==ConnectionState.Closed)
{
conn.Open();//打开数据库的连接
} }
//从数据库查找数据的方法
public DataSet query(string sql)
{
DataSet ds=new DataSet();//dataset是表的集合
SqlDataAdapter da=new SqlDataAdapter(sql,conn);//从数据库中查找
da.Fill(ds);//将数据填充到dataset
conn.Close();//关闭连接
return ds;//返回结果
}
//下边是更新数据库
public int updata(string sql)
{
SqlCommand cmd = new SqlCommand();//表示要对数据源执行的SQL语句或存储过程
cmd.CommandText = sql;//设置命令文本
cmd.CommandType = CommandType.Text;//设置命令的类型
cmd.Connection=conn;//设置命令的连接
int x=cmd.ExecuteNonQuery();//执行sql语句
conn.Close();//关闭连接
return x;//返回影响行数
}
//下边方法是关闭数据库连接
public void connclose()
{
if(conn.State==ConnectionState.Open)
{//判断数据库连接状态 如果打开 就将他关闭
conn.Close();
}
}
}protected void Page_Load(object sender, EventArgs e)
{
string source = @"Data Source=N\SQLEXPRESS;Initial Catalog=feiteng;Integrated Security=True";
SqlConnection conn = new SqlConnection(source);
conn.Open();
string select = "select name from gongsijianjie";
SqlCommand cmd = new SqlCommand(select,conn);
SqlDataReader reader = cmd.ExecuteReader();
if(reader.Read())
{
TextBox1.Text=reader["name"].ToString();
}
conn.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
if(this.TextBox1.Text=="")
{
Page.ClientScript.RegisterStartupScript(this.GetType(),"ok","alert('不能为空')",true);
}
string name = this.TextBox1.Text;
string sql = string.Format("update gongsijianjie set [name]='{0}'",name);
int y = new fangfa().updata(sql);
if (y > 0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "ok", "alert('修改成功!');", true);
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "ok", "alert('修改失败!');", true);
}
}
{
//
//TODO: 在此处添加构造函数逻辑
//
SqlConnection conn = null;//连接数据库的对象
//构造函数链接数据库 下
public fangfa()
{
if(conn==null)
{
conn=new SqlConnection();
conn.ConnectionString=@"Data Source=N\SQLEXPRESS;Initial Catalog=feiteng;Integrated Security=True";//连接数据库的字符串
}
if(conn.State==ConnectionState.Closed)
{
conn.Open();//打开数据库的连接
} }
//从数据库查找数据的方法
public DataSet query(string sql)
{
DataSet ds=new DataSet();//dataset是表的集合
SqlDataAdapter da=new SqlDataAdapter(sql,conn);//从数据库中查找
da.Fill(ds);//将数据填充到dataset
conn.Close();//关闭连接
return ds;//返回结果
}
//下边是更新数据库
public int updata(string sql)
{
SqlCommand cmd = new SqlCommand();//表示要对数据源执行的SQL语句或存储过程
cmd.CommandText = sql;//设置命令文本
cmd.CommandType = CommandType.Text;//设置命令的类型
cmd.Connection=conn;//设置命令的连接
int x=cmd.ExecuteNonQuery();//执行sql语句
conn.Close();//关闭连接
return x;//返回影响行数
}
//下边方法是关闭数据库连接
public void connclose()
{
if(conn.State==ConnectionState.Open)
{//判断数据库连接状态 如果打开 就将他关闭
conn.Close();
}
}
}protected void Page_Load(object sender, EventArgs e)
{
string source = @"Data Source=N\SQLEXPRESS;Initial Catalog=feiteng;Integrated Security=True";
SqlConnection conn = new SqlConnection(source);
conn.Open();
string select = "select name from gongsijianjie";
SqlCommand cmd = new SqlCommand(select,conn);
SqlDataReader reader = cmd.ExecuteReader();
if(reader.Read())
{
TextBox1.Text=reader["name"].ToString();
}
conn.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
if(this.TextBox1.Text=="")
{
Page.ClientScript.RegisterStartupScript(this.GetType(),"ok","alert('不能为空')",true);
}
string name = this.TextBox1.Text;
string sql = string.Format("update gongsijianjie set [name]='{0}'",name);
int y = new fangfa().updata(sql);
if (y > 0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "ok", "alert('修改成功!');", true);
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "ok", "alert('修改失败!');", true);
}
}
解决方案 »
- 求高手帮忙。看看这个sql语句有难度··
- 新手问题(c#怎么样请求网页)如:用代码点击百度的"百度一下"
- SQL2000 查询时间段问题
- 【紧】【急】UltraWebGrid的问题
- 用datareader 还是 datatable
- 验证控件与更新按钮同时操作时的冲突问题!
- 在VS2005中,做ASP.net的时候,为什么放上去的控件不可以托动,很不方便啊....这是为什么?
- 哪有asp.net+sqlserver 的电影网站的原码阿
- 为什么用localhost访问本地网站登录不了,而用IP地址192.168.1.*却可以?
- 关于Calendar日历控件的问题
- asp.net 如何实现阿拉伯数字转大写金额?
- 急用!在线等!ASP.NET控件悬浮变色JS代码
看看你的SQL语句到底执行没!!记住,一定要调试!
{
string source = @"Data Source=N\SQLEXPRESS;Initial Catalog=feiteng;Integrated Security=True";
SqlConnection conn = new SqlConnection(source);
conn.Open();
string select = "select name from gongsijianjie";
SqlCommand cmd = new SqlCommand(select,conn);
SqlDataReader reader = cmd.ExecuteReader();
if(reader.Read())
{
TextBox1.Text=reader["name"].ToString();
}
conn.Close();
}
改成
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)return;
string source = @"Data Source=N\SQLEXPRESS;Initial Catalog=feiteng;Integrated Security=True";
SqlConnection conn = new SqlConnection(source);
conn.Open();
string select = "select name from gongsijianjie";
SqlCommand cmd = new SqlCommand(select,conn);
SqlDataReader reader = cmd.ExecuteReader();
if(reader.Read())
{
TextBox1.Text=reader["name"].ToString();
}
conn.Close();
}