如何用vs2008在sql serve中建立一个数据库,并在网页中实现对其中一个表的增加和查找
3个回答
展开全部
protected void Button1_Click(object sender, EventArgs e)
{
string UserNameInput = TextBox1.Text;
string UserPasswordInput = TextBox2.Text;
string ReUserPasswordInput = TextBox3.Text;
//Get the content of the TextBox, and assgin.
SqlConnection conn = new SqlConnection
("Data Source=;initial catalog=;user id=sa;pwd =111111;");
//Create a connection to DataBase.
conn.Open();
string insert =
"insert into [Products Information] (ProductsName,ProductsRetailPrice,ProductsDiscountPrice) values(" + "'" + UserNameInput.Trim() + "'" + "如慎," + "'" + UserPasswordInput.Trim() + "'" + "," + "'" + ReUserPasswordInput.Trim() + "'" + ")";
SqlCommand cmd1 = new SqlCommand(insert, conn);
int i = cmd1.ExecuteNonQuery();
if (i != 0)
{
Response.Redirect("/AdminHomepage.aspx");
}
else
{
Response.Write("<script>alert('Do not add!')</script>");
}
conn.Close();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
//Cancel the operation.
}
public bool EditProduct(int ProductsID,string ProductsName,string ProductsRetailPrice,string ProductsDiscountPrice)
{
bool Edit;
SqlConnection conn = new SqlConnection
("Data Source=;initial catalog=;user id=sa;pwd =111111;");
conn.Open();
string editProducts = "Update [Products Information] set ProductsName=" + "'"皮橡升 + ProductsName.Trim() + "'" + "," + "ProductsRetailPrice=" + "'" + ProductsRetailPrice.Trim() + "'" + "," + "ProductsDiscountPrice=" + "'燃老" + ProductsDiscountPrice.Trim() + "'" + "where ProductsID=" + "'" + ProductsID + "'";
//Edit the content in the Products Information.
SqlCommand cmd1 = new SqlCommand(editProducts, conn);
int i = cmd1.ExecuteNonQuery();
if (i != 0)
{ Edit = true; }
else
{
Edit = false;
}
return Edit;
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int ProductsID1 = Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("Label1"))).Text);
string ProductsName1 = (GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox).Text;
string ProductsRetailPrice1 = ((GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox).Text);
string ProductsDiscountPrice1 = ((GridView1.Rows[e.RowIndex].FindControl("TextBox3") as TextBox).Text);
bool success=EditProduct(ProductsID1,ProductsName1,ProductsRetailPrice1,ProductsDiscountPrice1);
//Get the content of the TextBox, and assgin.
Response.Redirect("/AdminHomepage.aspx");
}
{
string UserNameInput = TextBox1.Text;
string UserPasswordInput = TextBox2.Text;
string ReUserPasswordInput = TextBox3.Text;
//Get the content of the TextBox, and assgin.
SqlConnection conn = new SqlConnection
("Data Source=;initial catalog=;user id=sa;pwd =111111;");
//Create a connection to DataBase.
conn.Open();
string insert =
"insert into [Products Information] (ProductsName,ProductsRetailPrice,ProductsDiscountPrice) values(" + "'" + UserNameInput.Trim() + "'" + "如慎," + "'" + UserPasswordInput.Trim() + "'" + "," + "'" + ReUserPasswordInput.Trim() + "'" + ")";
SqlCommand cmd1 = new SqlCommand(insert, conn);
int i = cmd1.ExecuteNonQuery();
if (i != 0)
{
Response.Redirect("/AdminHomepage.aspx");
}
else
{
Response.Write("<script>alert('Do not add!')</script>");
}
conn.Close();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
//Cancel the operation.
}
public bool EditProduct(int ProductsID,string ProductsName,string ProductsRetailPrice,string ProductsDiscountPrice)
{
bool Edit;
SqlConnection conn = new SqlConnection
("Data Source=;initial catalog=;user id=sa;pwd =111111;");
conn.Open();
string editProducts = "Update [Products Information] set ProductsName=" + "'"皮橡升 + ProductsName.Trim() + "'" + "," + "ProductsRetailPrice=" + "'" + ProductsRetailPrice.Trim() + "'" + "," + "ProductsDiscountPrice=" + "'燃老" + ProductsDiscountPrice.Trim() + "'" + "where ProductsID=" + "'" + ProductsID + "'";
//Edit the content in the Products Information.
SqlCommand cmd1 = new SqlCommand(editProducts, conn);
int i = cmd1.ExecuteNonQuery();
if (i != 0)
{ Edit = true; }
else
{
Edit = false;
}
return Edit;
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int ProductsID1 = Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("Label1"))).Text);
string ProductsName1 = (GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox).Text;
string ProductsRetailPrice1 = ((GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox).Text);
string ProductsDiscountPrice1 = ((GridView1.Rows[e.RowIndex].FindControl("TextBox3") as TextBox).Text);
bool success=EditProduct(ProductsID1,ProductsName1,ProductsRetailPrice1,ProductsDiscountPrice1);
//Get the content of the TextBox, and assgin.
Response.Redirect("/AdminHomepage.aspx");
}
展开全部
直接在vs2008的界面中点击dataview按钮 直接链接到你数据库的地址 调正属性 就行了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
直接用那个 dataview啥的 直接链接 调调属性 就ok了。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询