CSDN博客

img tingningpower

insert into data.mdb

发表于2003/3/16 12:55:00  2470人阅读

分类: ASP.NET

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<html>
 
<script language="C#" runat="server">

   
    protected void Page_Load(Object Src, EventArgs E)
    {
        string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
        strConnection += @"Data Source=//genfs1/www11/etning/xml/Northwind.mdb";
   
        if (!IsPostBack)
            BindGrid();
    }

    public void AddEmployees_Click(Object sender, EventArgs E)
    {
        if (EmployeeID.Value == "" || LastName.Value == "" || FirstName.Value == "" || HomePhone.Value == "")
        {
          Message.InnerHtml = "eer:“EmployeeID”、“LastName”OR“HomePhone”no hhhhhhong";
          Message.Style["color"] = "red";
          BindGrid();
          return;
        }

        String insertstr = "insert into Employees (EmployeeID, LastName, FirstName, HomePhone, Address, City, Country, Notes, PostalCode) values (@EmployeeID, @LastName, @FirstName, @HomePhone, @Address, @City, @Country, @Notes, @PostalCode)";
        OleDbCommand strCommand = new OleDbCommand(insertstr,strConnection);
  
        int cmdresults = 0;
        OleDbCommand2.CommandText = "NewAuthor";
        OleDbCommand2.CommandType = CommandType.StoredProcedure;
        OleDbCommand2.Parameters["EmployeeID"].Value = TextBox1.Text;
        OleDbCommand2.Parameters["LastName"].Value = TextBox2.Text;
        OleDbCommand2.Parameters["FirstName"].Value = TextBox3.Text;
        OleDbCommand2.Parameters["HomePhone"].Value = TextBox4.Text;
        OleDbCommand2.Parameters["Address"].Value = TextBox5.Text;
        OleDbCommand2.Parameters["City"].Value = TextBox6.Text;
        OleDbCommand2.Parameters["Country"].Value = TextBox7.Text;
        OleDbCommand2.Parameters["Notes"].Value = TextBox8.Text;
        OleDbCommand2.Parameters["PostalCode"].Value = CheckBox1.Checked;

        OleDbConnection2.Open();
        try
       {
            cmdresults = OleDbCommand2.ExecuteNonQuery();
        }
        catch (Exception ex)
       {
              MessageBox.Show("Failed to execute command, err = " + ex.Message);
        }
       OleDbConnection2.Close();
       MessageBox.Show("Number of records inserted = " + cmdresults.ToString());

        myCommand.Connection.Close();

        BindGrid();
    }

    public void BindGrid()
    {
        string strSQL = "SELECT * FROM Employees";
    DataSet objDataSet = new DataSet();
    OleDbConnection objConnection = new OleDbConnection(strConnection);
    OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL, objConnection);
    objAdapter.Fill(objDataSet, "Employees");
    DataView objDataView = new DataView(objDataSet.Tables["Employees"]);
    MyDataGrid.DataSource=objDataView;
    MyDataGrid.DataBind();
    }

</script>

<body style="font: 10.5pt 宋体">

  <form runat="server">

    <h3><font face="宋体">插入数据行</font></h3>

    <table width="95%">
      <tr>
        <td valign="top">

          <ASP:DataGrid id="MyDataGrid" runat="server"
            Width="700"
            BackColor="#ccccff"
            BorderColor="black"
            ShowFooter="false"
            CellPadding=3
            CellSpacing="0"
            Font-Name="宋体"
            Font-Size="8pt"
            HeaderStyle-BackColor="#aaaadd"
            EnableViewState="false"
          />

        </td>
        <td valign="top">

          <table style="font: 9pt 宋体">
            <tr>
              <td colspan="2" bgcolor="#aaaadd" style="font:10.5pt 宋体">添加新作者:</td>
            </tr>
            <tr>
              <td nowrap>作者 ID:</td>
              <td><input type="text" id="EmployeeID" value="000-00-0000" runat="server"></td>
            </tr>
            <tr>
              <td nowrap>姓氏:</td>
              <td><input type="text" id="LastName" value="Doe" runat="server"></td>
            </tr> 
            <tr nowrap>
              <td>名字:</td>
              <td><input type="text" id="FirstName" value="John" runat="server"></td>
            </tr>
            <tr>
              <td>电话:</td>
              <td><input type="text" id="HomePhone" value="808 555-5555" runat="server"></td>
            </tr>
            <tr>
              <td>地址:</td>
              <td><input type="text" id="Address" value="One Microsoft Way" runat="server"></td>
            </tr>
            <tr>
              <td>城市:</td>
              <td><input type="text" id="City" value="Redmond" runat="server"></td>
            </tr>
            <tr>
              <td>州:</td>
              <td>
                <select id="Country" runat="server">
                  <option>CA</option>
                  <option>IN</option> 
                  <option>KS</option> 
                  <option>MD</option> 
                  <option>MI</option> 
                  <option>OR</option>
                  <option>TN</option> 
                  <option>UT</option> 
                </select>
              </td>
            </tr>
            <tr>
              <td nowrap>邮政编码:</td>
              <td><input type="text" id="Notes" value="98005" runat="server"></td>
            </tr>
            <tr>
              <td>PostalCode:</td>
              <td>
                <select id="PostalCode" runat="server">
                  <option value="0">假</option>
                  <option value="1">真</option>
                </select>
              </td>
            </tr>
            <tr>
              <td></td>
              <td style="padding-top:15">
                <input type="submit" OnServerClick="AddEmployees_Click" value="添加作者" runat="server">
              </td>
            </tr>
            <tr>
              <td colspan="2" style="padding-top:15" align="center">
                <span id="Message" EnableViewState="false" style="font: arial 11pt;" runat="server"/>
              </td>
            </tr>
          </table>

        </td>
      </tr>
    </table>

  </form>

</body>
</html>

里面有部分错误,请各位高手指点,谢谢!


连接到演示
阅读全文
0 0

相关文章推荐

img
取 消
img