ASP.NETを用いたDB連携
ASP.NETを用いた会員情報登録ページのDB連結について
現在ASP.NETを用いてログイン機能の勉強をしています。
その際にデータの受け渡しをリテラルで行いSQLserverに登録するページを作っています。
パスワード、ログインID、氏名を登録したいのですがうまくいかなかった為、投稿させていただきました。
発生している問題・エラー
System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'Table'.'
該当するソースコード
torokukakunin.aspx
using System;
using System.Configuration;
using System.Data.SqlClient;
public partial class torokukakunin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Literal1.Text = (String)Session["pass"];
Literal2.Text = (String)Session["ID"];
Literal3.Text = (String)Session["name"];
}
protected void Button1_Click(object sender, EventArgs e)
{
var connectionString = ConfigurationManager.ConnectionStrings["bbs"].ConnectionString; //[]ないを自分のDBネームにする
using (var connection = new SqlConnection(connectionString))
using (var command = connection.CreateCommand())
{
try
{
// データベースの接続開始
connection.Open();
// SQLの準備
command.CommandText = @"INSERT INTO Table (pass, loginID, name) VALUES (@pass, @loginID, @name)";
command.Parameters.Add(new SqlParameter("@pass", Literal1.Text)); //リテラル内を@パラメータに変換している。
command.Parameters.Add(new SqlParameter("@loginID", Literal2.Text));
command.Parameters.Add(new SqlParameter("@name", Literal3.Text));
// SQLの実行
command.ExecuteNonQuery();
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
throw;
}
finally
{
// データベースの接続終了
connection.Close();
}
}
}
}
こちらの画面からデータを入力し、torokukakunin.aspxに遷移しDBに登録となっております。
kaintouroku.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/bbs.master" AutoEventWireup="true" CodeFile="kaintouroku.aspx.cs" Inherits="kaintouroku" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Label ID="Label1" runat="server" Text="パスワード"></asp:Label>
<br/>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="ログインID"></asp:Label>
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label3" runat="server" Text="氏名"></asp:Label>
<br />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="登録" OnClick="Button1_Click" />
</asp:Content>
ssmsというものを使いDBとの接続を行っています。
コントロールパネルでもSQLserver(express)は起動となっております。
変数の確認、IDの確認など。
宜しくお願いします。
0 likes