ログイン型の掲示板を作成しています。
開発環境はWindows visual studio 2017 バージョン4.5.2 C#です。
アカウント登録をSQL serverを使って開発しているのですが行き詰ってしまって質問させていただいています。
※visual studio、C#での開発は初めてです。
web.conflgの内容
<connectionStrings> <add name="sqlsvr" connectionString="Data Source=(LocalDB)\MSSQLLocalDB; AttachDBFilename=C:\Users\ikunao\Documents\Visual Studio 2017\WebSites\WebSite1\App_Data\DB.mdf;Integrated Security=false;(.mdfのプロパティにある接続文字列をコピペしたのですができません。 Integrated Security=false"/> </connectionStrings>
ボタンをクリックしたときのアクション
(sqlserverに接続してクエリを追加する処理がしたい)
protected void Unnamed1_Click(object sender, EventArgs e) { // 接続文字列の取得 ###var connectionString = ConfigurationManager.ConnectionStrings["sqlsvr"].ConnectionString; ↑で[ConfigurationErrorsException]: 構成セクションは、CDATA、あるいはテキスト要素を含むことはできません' というエラーが発生する using (var connection = new SqlConnection(connectionString)) using (var command = connection.CreateCommand()) { try { // データベースの接続開始 connection.Open(); // SQLの準備 command.CommandText = @"INSERT INTO Account (Id, Name, Pass) VALUES (@ID, @PASSWORD, @ROLE_NAME)"; command.Parameters.Add(new SqlParameter("@ID", Id)); command.Parameters.Add(new SqlParameter("@PASSWORD", Name)); command.Parameters.Add(new SqlParameter("@ROLE_NAME", Pass)); // SQLの実行 command.ExecuteNonQuery(); } catch (Exception exception) { Console.WriteLine(exception.Message); throw; } finally { // データベースの接続終了 connection.Close(); } Response.Redirect("./AddNewMemberCheck.aspx"); } }
回答2件
あなたの回答
tips
プレビュー