2011-10-19 53 views
0

我是新来的ASP。ASP按钮onClick发送数据从文本框到数据库

我想从asp:文本框插入数据到SQL Server数据库。 我已经在处理数据库连接和插入方法的APP_CODE文件夹中创建了一个类。

当button1被点击时,我希望它从asp:文本框中获取数据并使用APP_CODE中的questionnaireSQL.cs文件来更新数据库。

我该怎么办?

questionnaireSQL.cs

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Data.SqlClient; 
using System.Data; 

namespace Devworks 
{ 

    public class OscarSQL 
    { 
     private string _productConnectionString; 
     private SqlConnection _productConn; 

     public OscarSQL() 
     { 
      _productConn = new SqlConnection(); 
      _productConnectionString += "data source=mssql.dev-works.co.uk; Initial Catalog=devworks_oscar;User ID=myusername;Password=mypassword"; 
      _productConn.ConnectionString = _productConnectionString; 
     } 

    // Insert New Questionnaire: 

     public int InsertQuestionnaire(string QuestName, int CustomerID, int NumberOfQuest) 
     { 
      int retVal = 0; 
      SqlCommand myCommand = new SqlCommand("NewQuestionnaire", _productConn); 
      myCommand.CommandType = CommandType.StoredProcedure; 
      myCommand.Parameters.Add(new SqlParameter("@QUESTNAME", SqlDbType.NVarChar)); 
      myCommand.Parameters.Add(new SqlParameter("@CUSTID", SqlDbType.Int)); 
      myCommand.Parameters.Add(new SqlParameter("@NUMQUEST", SqlDbType.Int)); 
      myCommand.Parameters.Add("@QUEST_ID", SqlDbType.Int, 0, "QUEST_ID"); 
      myCommand.Parameters["@QUEST_ID"].Direction = ParameterDirection.Output; 
      myCommand.Parameters[0].Value = QuestName; 
      myCommand.Parameters[1].Value = CustomerID; 
      myCommand.Parameters[2].Value = NumberOfQuest; 
      _productConn.Open(); 
      myCommand.ExecuteNonQuery(); 
      retVal = (int)myCommand.Parameters["@QUEST_ID"].Value; 
      _productConn.Close(); 
      return retVal; 
     } 


     // SQL DATAREADER, DATATABLE & NON QUERY UPDATE: 

     private void insert(SqlCommand myCommand) 
     { 
      _productConn.Open(); 
      myCommand.ExecuteNonQuery(); 
      _productConn.Close(); 
     } 

     private SqlDataReader getData(SqlCommand myCommand) 
     { 
      _productConn.Open(); 
      SqlDataReader myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection); 
      return myDataReader; 
     } 

     private DataTable createDataTable(SqlDataReader data) 
     { 
      DataTable retVal = new DataTable(); 
      retVal.Load(data); 
      return retVal; 
     } 
    } 
} 

newquestionnaire.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Main.Master" AutoEventWireup="true" CodeBehind="new_questionnaire.aspx.cs" Inherits="new_questionnaire" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> 
    <div class="container"> 
     <div class="main"> 
      <h2 class="new">Add Your Questionnaire</h2> 
      <h3>Give your questionnaire a name.</h3> 
      <asp:TextBox ID="QuestName" runat="server"></asp:TextBox> 
      <h3>CustomerID</h3> 
      <asp:TextBox ID="CustomerID" runat="server"></asp:TextBox> 
      <h3>Number Of Questions</h3> 
      <asp:TextBox ID="NumberOfQuest" runat="server"></asp:TextBox> 
      <br /> 
      <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Create" /> 
     </div> <!-- end main --> 
    </div> <!-- end container --> 
</asp:Content> 

newquestionnaire.aspx.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 
using Devworks; 

    public partial class new_questionnaire : System.Web.UI.Page 
    { 


      protected void Page_Load(object sender, EventArgs e) 
      { 

      } 

      protected void Button1_Click(object sender, EventArgs e) 
      { 

      } 
     } 

在此先感谢

回答

2

你已经发布了所有这些代码,但确切地说你不能做什么? 一个人不得不怀疑你是否编写了DAL(数据访问层 - 你的db代码)和Web UI,你怎么不能写一个讽刺地叫做Button1的按钮的点击事件?

您将文本框的值和点击按钮的值插入到数据库中。 当你有一个大任务将它分成较小的任务,并且完成每一件直到你完成拼图。

根据你的代码

OscarSQL c = new OscarSQL(); //btw: not sure why you are not using a static class for this 
int result = c.InsertQuestionnaire(textBox.Text ...); //add all parameters here 

If (result > 0) 
    //good id 
else 
    //display error message 

将所有的button1_click事件代码的背后代码。

+0

谢谢。这有我想要的结果。 我的目标是使用.aspx页面中的值并解析App_Code中.cs文件中的方法 – HGomez90

相关问题