C#中可以用控件sqldatasource和gridview进行删除更新操作,但是我怎么总是在设置参数问题那出问题
展开全部
public partial class gridview_GridViewUp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridViewBind();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridViewBind();
}
private void GridViewBind()
{
string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
string SqlStr = "Select a.*,b.description FROM test01 a,xueli b where a.xueli=b.code and a.id<1000 and a.id>200";
DataSet ds = new DataSet();
try
{
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds, "test01");
if (conn.State.ToString() == "Open") conn.Close();
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (((DropDownList)e.Row.FindControl("DDLXueli")) != null)
{
DropDownList ddlxueli = (DropDownList)e.Row.FindControl("DDLXueli");
// 生成 DropDownList 的值,绑定数据
string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
string SqlStr = "Select * from xueli";
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds, "xueli");
if (conn.State.ToString() == "Open") conn.Close();
ddlxueli.DataSource = ds.Tables[0].DefaultView;
ddlxueli.DataTextField = "description";
ddlxueli.DataValueField = "code";
ddlxueli.DataBind();
//
// 选中 DropDownList
ddlxueli.SelectedValue = ((HiddenField)e.Row.FindControl("HDFXueli")).Value;
//
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridViewBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridViewBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
string card = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TBCard")).Text;
string xueli = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLXueli")).SelectedValue;
string price = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TBPrice")).Text;
string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
string SqlStr = "update test01 set card='" + card + "',xueli='" + xueli + "',price='" + price + "' where id=" + id;
try
{
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
comm.ExecuteNonQuery();
comm.Dispose();
if (conn.State.ToString() == "Open") conn.Close();
GridView1.EditIndex = -1;
GridViewBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
string SqlStr = "delete from test01 where id=" + id;
try
{
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
comm.ExecuteNonQuery();
comm.Dispose();
if (conn.State.ToString() == "Open") conn.Close();
GridView1.EditIndex = -1;
GridViewBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
}
sql server2000生成表代码:
Create TABLE [dbo].[test01] (
[id] [decimal](18, 0) IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[card] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[xueli] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[price] [decimal](18, 0) NULL ,
[createdate] [datetime] NULL
) ON [PRIMARY]
GO
Alter TABLE [dbo].[test01] ADD
CONSTRAINT [DF_test01_createdate] DEFAULT (getdate()) FOR [createdate],
CONSTRAINT [PK_test01] PRIMARY KEY CLUSTERED
(
[id]
) ON [PRIMARY]
GO
Create TABLE [dbo].[xueli](
[id] [int] IDENTITY(1,1) NOT NULL,
[code] [int] NULL,
[description] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
CONSTRAINT [PK_xueli] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridViewBind();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridViewBind();
}
private void GridViewBind()
{
string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
string SqlStr = "Select a.*,b.description FROM test01 a,xueli b where a.xueli=b.code and a.id<1000 and a.id>200";
DataSet ds = new DataSet();
try
{
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds, "test01");
if (conn.State.ToString() == "Open") conn.Close();
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (((DropDownList)e.Row.FindControl("DDLXueli")) != null)
{
DropDownList ddlxueli = (DropDownList)e.Row.FindControl("DDLXueli");
// 生成 DropDownList 的值,绑定数据
string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
string SqlStr = "Select * from xueli";
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds, "xueli");
if (conn.State.ToString() == "Open") conn.Close();
ddlxueli.DataSource = ds.Tables[0].DefaultView;
ddlxueli.DataTextField = "description";
ddlxueli.DataValueField = "code";
ddlxueli.DataBind();
//
// 选中 DropDownList
ddlxueli.SelectedValue = ((HiddenField)e.Row.FindControl("HDFXueli")).Value;
//
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridViewBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridViewBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
string card = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TBCard")).Text;
string xueli = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLXueli")).SelectedValue;
string price = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TBPrice")).Text;
string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
string SqlStr = "update test01 set card='" + card + "',xueli='" + xueli + "',price='" + price + "' where id=" + id;
try
{
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
comm.ExecuteNonQuery();
comm.Dispose();
if (conn.State.ToString() == "Open") conn.Close();
GridView1.EditIndex = -1;
GridViewBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
string SqlStr = "delete from test01 where id=" + id;
try
{
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
comm.ExecuteNonQuery();
comm.Dispose();
if (conn.State.ToString() == "Open") conn.Close();
GridView1.EditIndex = -1;
GridViewBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
}
sql server2000生成表代码:
Create TABLE [dbo].[test01] (
[id] [decimal](18, 0) IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[card] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[xueli] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[price] [decimal](18, 0) NULL ,
[createdate] [datetime] NULL
) ON [PRIMARY]
GO
Alter TABLE [dbo].[test01] ADD
CONSTRAINT [DF_test01_createdate] DEFAULT (getdate()) FOR [createdate],
CONSTRAINT [PK_test01] PRIMARY KEY CLUSTERED
(
[id]
) ON [PRIMARY]
GO
Create TABLE [dbo].[xueli](
[id] [int] IDENTITY(1,1) NOT NULL,
[code] [int] NULL,
[description] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
CONSTRAINT [PK_xueli] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
展开全部
个人觉得是不是你正在连接SQL数据库?下面这句代码就是删除更新操作的
dataGridView_CX.Rows.Remove(dataGridView_CX.SelectedRows[i-1]);//count为总数
}
try
{
sqlDataAdapter1.Update(dataSet11);//这个就是执行在删除后再上传至数据库
MessageBox.Show("删除成功");
}
如果这不是你想要的答案,那么请说明你是只是在VS上面设置参数出问题还是数据库方面?
dataGridView_CX.Rows.Remove(dataGridView_CX.SelectedRows[i-1]);//count为总数
}
try
{
sqlDataAdapter1.Update(dataSet11);//这个就是执行在删除后再上传至数据库
MessageBox.Show("删除成功");
}
如果这不是你想要的答案,那么请说明你是只是在VS上面设置参数出问题还是数据库方面?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询