今天來簡單紀錄一下ASP.NET與SQL SERVER之間如何進行資料的傳遞。
網頁上分別有ID與TEXT讓使用者輸入,以及讓使用者輸入ID可以查詢TEXT。
由於只是範例,並沒有做空值或ID有沒有重複的判斷。
由於只是範例,並沒有做空值或ID有沒有重複的判斷。
在SQL SERVER建置一個Table,有2個欄位,ID與Text。
aspx 程式碼
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> | |
<!DOCTYPE html> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head runat="server"> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title></title> | |
</head> | |
<body> | |
<form id="form1" runat="server"> | |
<div> | |
<asp:Label ID="Label1" runat="server" Text="ID" Width="40px"></asp:Label> | |
<asp:TextBox ID="txtInsert_ID" runat="server" Width="71px"></asp:TextBox> | |
</div> | |
<div> | |
<asp:Label ID="Label2" runat="server" Text="TEXT" Width="40px"></asp:Label> | |
<asp:TextBox ID="txtInsert_TEXT" runat="server"></asp:TextBox> | |
</div> | |
<div> | |
<asp:Button ID="BtnInsert" runat="server" OnClick="BtnInsert_Click" Text="Insert" /> | |
</div> | |
<br /> | |
<br /> | |
<div> | |
<asp:Label ID="Label3" runat="server" Text="ID" Width="40px"></asp:Label> | |
<asp:TextBox ID="txtSearch_ID" runat="server" Width="71px" ></asp:TextBox> | |
</div> | |
<div> | |
<asp:Label ID="Label4" runat="server" Text="TEXT" Width="40px"></asp:Label> | |
<asp:TextBox ID="txtSearch_TEXT" runat="server" Enabled="False"></asp:TextBox> | |
</div> | |
<div> | |
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" /> | |
</div> | |
</form> | |
</body> | |
</html> |
.cs 程式碼
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Data.SqlClient; | |
public partial class _Default : System.Web.UI.Page | |
{ | |
string sqlConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SQL_ConnectionString"].ConnectionString; | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
} | |
protected void BtnInsert_Click(object sender, EventArgs e) | |
{ | |
string sql = "insert into testconnect(ID,TEXT) values(@id,@text)"; | |
using (SqlConnection con = new SqlConnection(sqlConnectionString)) | |
{ | |
using (SqlCommand cmd = new SqlCommand()) | |
{ | |
cmd.Connection = con; | |
cmd.CommandText = sql; | |
cmd.Parameters.AddWithValue("@id", txtInsert_ID.Text); | |
cmd.Parameters.AddWithValue("@text",txtInsert_TEXT.Text); | |
con.Open(); | |
cmd.ExecuteNonQuery(); | |
con.Close(); | |
} | |
} | |
} | |
protected void btnSearch_Click(object sender, EventArgs e) | |
{ | |
string sql = "select text from ASPNET.dbo.testconnect where ID = @id"; | |
using (SqlConnection con = new SqlConnection(sqlConnectionString)) | |
{ | |
using (SqlCommand cmd = new SqlCommand()) | |
{ | |
cmd.Connection = con; | |
cmd.CommandText = sql; | |
cmd.Parameters.AddWithValue("@id", txtSearch_ID.Text); | |
con.Open(); | |
using (SqlDataReader sdr = cmd.ExecuteReader()) | |
{ | |
if (sdr.HasRows) | |
{ | |
while (sdr.Read()) | |
{ | |
txtSearch_TEXT.Text = sdr.GetString(0); | |
} | |
} | |
else | |
{ | |
txtSearch_TEXT.Text = "無此ID資料!"; | |
} | |
} | |
con.Close(); | |
} | |
} | |
} | |
} |