[求助]用C#将数据插入SQL的问题
我用ASP.NET平台建了一个图书馆系统,正在做插入书籍的功能,用了一些TextBox控件,如果我想在这些控件里输入相关信息,然后单击提交就能在SQLSERVER的表里加入数据,应该怎样写代码?请指点.
我把一个表单的内容提交到一个.aspx的页面去处理:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="insert.aspx.cs" Inherits="insert" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server" method="post" action="Sinsert.aspx">
<div>
<asp:Label ID="Label1" runat="server" Style="left: 108px; position: relative; top: 134px"
Text="请填写以下信息:"></asp:Label>
<table style="left: 106px; width: 446px; position: relative; top: 153px; height: 138px" border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="width: 60px">
书 号</td>
<td style="width: 155px">
<asp:TextBox ID="insid" runat="server" Style="position: relative; left: 0px; top: -1px;"></asp:TextBox></td>
<td style="width: 51px">
作 者</td>
<td>
<asp:TextBox ID="inswriter" runat="server" Style="position: relative"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 60px">
书 名</td>
<td style="width: 155px">
<asp:TextBox ID="insname" runat="server" Style="position: relative"></asp:TextBox></td>
<td style="width: 51px">
种 类</td>
<td>
<asp:TextBox ID="inssort" runat="server" Style="position: relative"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 60px">
版 本</td>
<td style="width: 155px">
<asp:TextBox ID="insedition" runat="server" Style="position: relative"></asp:TextBox></td>
<td style="width: 51px">
出版社</td>
<td>
<asp:TextBox ID="inspublisher" runat="server" Style="position: relative"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 60px">
日 期</td>
<td style="width: 155px">
<asp:TextBox ID="inspub" runat="server" Style="position: relative"></asp:TextBox></td>
<td style="width: 51px">
价 格</td>
<td>
<asp:TextBox ID="insprice" runat="server" Style="position: relative"></asp:TextBox></td>
</tr>
</table>
</div>
<asp:Button ID="Button1" runat="server" Style="left: 220px; position: relative; top: 189px"
Text="提交" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Style="left: 381px; position: relative; top: 190px"
Text="清空" OnClick="Button2_Click"/>
</form>
</body>
</html>
然后在.aspx页的代码是:
string insid = Request["insid"].ToString();
string insname = Request["insname"].ToString();
string insedition = Request["insedition"].ToString();
string inswriter = Request["inswriter"].ToString();
string inssort = Request["inssort"].ToString();
string inspublisher = Request["inspublisher"].ToString();
string inspub = Request["inspub"].ToString();
string insprice = Request["insprice"].ToString();
SqlConnection con = DB.Connection();
con.Open();
SqlCommand cmd = new SqlCommand("insert into dbo.Books (BooksID,BookName,Edition,AuthorsName,Sort,PublisherName,PubData,Price) values(" + insid + ",'" + insname + "','" + insedition + "','" + inswriter + "','" + inssort + "','" + inspublisher + "','" + inspub + "'," + insprice + ")", con);
int num = cmd.ExecuteNonQuery();
if (num == 1)
{
Response.Write("<script>alert('插入成功!');</script>");
}
else
{
Response.Write("<script>alert('插入失败!');</script>");
}
con.Close();
运行通过,可是在SQLSERVER里面没有发现插入数据,哪里错了吗?