数据库的简单操作-学习记录

后台程序:
        ///<summary>
        ///数据库操作摘要
        ///</summary>
        SqlConnection sql = new SqlConnection("server=(local)\\SQLEXPRESS;database=pubs;Trusted_Connection = true");
        string str = "INSERT INTO auther (au_name,message) VALUES(@au_name,@message)";
        SqlCommand cmd = new SqlCommand(str,sql);
        cmd.Parameters.Add("@au_name", SqlDbType.VarChar, 20);
        cmd.Parameters.Add("@message", SqlDbType.VarChar,300);
        cmd.Parameters["@au_name"].Value = au_name.Text.ToString();
        cmd.Parameters["@message"].Value = message.Text.ToString();
        sql.Open();
        cmd.ExecuteNonQuery();
        sql.Close();
前台程序:
<body>
    <form id="form1" runat="server">
    <div>
    <center>
        <h3>NoteBook</h3>
        Name:<asp:TextBox ID="au_name" runat="server" />
        <p />
        Phone:<asp:TextBox ID="message" runat="server" TextMode="MultiLine"
                Height="183px" Width="296px"/>
        <p />
        <asp:Button ID="insertMessage" runat="server" Text="Send"
                onclick="insertMessage_Click"/>
        <p />
      
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
                AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
                DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" Width="296">
                <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                <Columns>
                    <asp:CommandField ShowSelectButton="True" />
                    <asp:BoundField DataField="au_name" HeaderText="au_name"
                        SortExpression="au_name" />
                    <asp:BoundField DataField="message" HeaderText="message" SortExpression="message" />
                </Columns>
                <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
                SelectCommand="SELECT [au_name], [message] FROM [auther]">
            </asp:SqlDataSource>
      
    </center>
    </div>
    </form>

你可能感兴趣的:(.net,数据库,代码,程序,休闲)