ASP按钮onClick将数据从文本框发送到数据库
我是 ASP 新手。
我正在尝试将数据从 asp:textbox 插入 SQL Server 数据库。 我在 APP_CODE 文件夹中创建了一个类,用于处理数据库连接和插入方法。
单击 Button1 时,我希望它从 asp:textbox 获取数据并使用 APP_CODE 中的调查问卷SQL.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)
{
}
}
提前致谢
I'm new to ASP.
I am trying to insert data from an asp:textbox into a SQL Server database.
I have created a class in the APP_CODE folder which handles the database connection and insert methods.
When button1 is clicked I want it to take the data from the asp:textbox and use the questionnaireSQL.cs file in APP_CODE to update the database.
How can I go about this?
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)
{
}
}
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经发布了所有这些代码,但到底有什么是您不能做的呢?
人们不禁要问,如果您编写了 DAL(数据访问层 - 您的数据库代码)和 Web UI,为什么您无法编写一个讽刺性地称为
Button1
的按钮的单击事件?您获取文本框的值,然后单击按钮将其插入数据库中。
当你有一个大任务时,将其分成较小的任务并完成每个部分,直到完成拼图。
根据您的代码
将所有这些代码放在代码的
button1_click
事件后面。You've posted all this code but exactly what can't you do?
One has to wonder if you wrote the DAL (Data access layer - your db code) and the Web UI, how is it that you cannot write the click event of a button ironically called
Button1
??You take the value of your textbox and on the click of a button insert it into a database.
When you have a large task seperate it into smaller ones and do each piece until you've finished the puzzle.
According to your code
Place all of this code behind the
button1_click
event of your code.