首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在visual studio中不使用localdb的连接

在visual studio中不使用localdb的连接
EN

Stack Overflow用户
提问于 2015-04-21 00:53:51
回答 1查看 202关注 0票数 0

很抱歉,我问这个问题,我知道还有很多其他的问题,并试图使用所提供的解决方案,但我只是无法使我的代码工作。谢谢你找我!

连接字符串如Properties中所示:

Data Source=(LocalDB)\v11.0;AttachDbFilename="C:\Users\Jacob\Documents\Visual Studio 2013\Projects\WindowsFormsApplication2\WindowsFormsApplication2\ChatDB.mdf";Integrated Security=True

App.config中的连接字符串:

数据Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\ChatDB.mdf;Integrated Security=True

代码语言:javascript
复制
Error: An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Incorrect syntax near the keyword 'User'.

代码:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//NC-1 More namespaces.
using System.Data.SqlClient;
using System.Configuration;

namespace WindowsFormsApplication2
{
    public partial class SignUp : Form
    {
        string connstr = ConfigurationManager.ConnectionStrings["WindowsFormsApplication2.Properties.Settings.ChatDBConnectionString"].ToString();

        public SignUp()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void SubmitBtn_Click(object sender, EventArgs e)
        {
            string Name = NameText.Text;
            string Pwd = PwdText.Text;
            //make sure they have entered text
            if (Name.Length > 0 && Pwd.Length > 0)
            {
               SqlConnection conn = new SqlConnection(connstr);

                //NC-10 try-catch-finally
                try
                {
                    //NC-11 Open the connection.
                    conn.Open();

                    SqlCommand insert = new SqlCommand();
                    insert.Connection = conn;
                    insert.CommandText = "INSERT INTO [User] (Name,Password) VALUES ('" + Name + "','" + Pwd + "')";

                    insert.ExecuteNonQuery();
                    MessageBox.Show("Congrats!!!");

                }
                catch
                {
                    //NC-14 A simple catch.

                    MessageBox.Show("User was not returned. Account could not be created.");
                }
                finally
                {
                    //NC-15 Close the connection.
                    conn.Close();
                }
            }
            //if no text make them enter
            else
            {
                MessageBox.Show("Please enter Text in both fields.");
            }
        }
    }
}

再次感谢你的寻找。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-21 01:11:04

问题在于您的SQL查询,因为您使用的是保留关键词

尝试将表名更改为tblUser

我还建议使用参数化查询来防止将来的SQL注入:(例如)

代码语言:javascript
复制
@"INSERT INTO [User] (Name,Password) VALUES (@Name, @Password);"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29760999

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档