简体   繁体   English

Visual Studio-无法导入此数据库

[英]Visual Studio - This database cannot be imported

I'm trying to insert an entry into a table in visual studio, but after I run my code and then try to view the table I get this error message, 我正在尝试在Visual Studio中的表中插入一个条目,但是在运行代码然后尝试查看表后,出现此错误消息,

This database cannot be imported. 该数据库无法导入。 It is either an unsupported SQL server version or an unsupported database compatibility. 它是不受支持的SQL Server版本或不受支持的数据库兼容性。

Here is the code that is attempting to insert, 这是尝试插入的代码,

private void doneButton_Click(object sender, EventArgs e) {
    string userName = userNameTextBox.Text,             
        password = passwordTextBox.Text,
        question = questionMenu.Text,
        answer = answerTextBox.Text;
        int key = EncryptionClass.generateKey();

    SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MyData.mdf;Integrated Security=True");

    connection.Open();

    String sqlQuery = "INSERT INTO dbo.Account(UserName, UserPassword, UserKey) " +    
           "VALUES (\'" + userName + "\', \'" + EncryptionClass.encrypt(password, key) + "\', " + key + ");";

    Console.WriteLine("string " + sqlQuery);
    // INSERT INTO dbo.Account(UserName, UserPassword, UserKey) VALUES ('victoramaro', 'obvmhkT1', 19);

    using (SqlCommand command = new SqlCommand(sqlQuery, connection)) {
        try {
            var res = command.ExecuteNonQuery();
        } catch (SqlException ex) {
            Console.WriteLine(ex.Message);
        }
    }
        connection.Close();
}

And the App.config, 还有App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="Assign6.Properties.Settings.MyDataConnectionString"
            connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MyData.mdf;Integrated Security=True"
            providerName="System.Data.SqlClient" />   
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
</configuration>

In the properties of MyData.mdf Build Action is set to Content, Copy to Output Directory is set to Copy if newer. 在MyData.mdf的属性中,将“构建操作”设置为“内容”,将“复制到输出目录”设置为“复制”(如果较新)。

In the properties of MyDataDataSet.xsd Build Action is set to None, Copy to Output Directory is set to Do not copy. 在MyDataDataSet.xsd的属性中,将“构建操作”设置为“无”,将“复制到输出目录”设置为“不复制”。

EDIT 编辑

SqlConnection connection = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\MyData.mdf;Integrated Security=True");

connection.Open();


String sqlQuery = "INSERT INTO dbo.Account(UserName, UserPassword, UserKey) " +     //create insert query to insert user data into Account table
                        "VALUES (\'" + userName + "\', \'" + EncryptionClass.encrypt(password, key) + "\', " + key + ");";

using (SqlCommand command = new SqlCommand(sqlQuery, connection)) {
        try {
           command.ExecuteNonQuery();



            string selectStatement = "SELECT * FROM Account";

            SqlCommand selectCommand = new SqlCommand(selectStatement, connection);
            SqlDataReader sqlReader = selectCommand.ExecuteReader();             //execute the query 

            while (sqlReader.Read()) {                                         //while reader has data
                string outString = string.Empty;
                for (int k = 0; k < sqlReader.FieldCount; k++) {                //go throught the field count
                    outString += String.Format("{0, -8}", sqlReader[k]);        //add item to string
                }
                Console.WriteLine(outString);
            }

        }
        catch (SqlException ex) {
            throw ex;
        }
        finally {
            connection.Close();
        }
    }

in here 在这里

    SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MyData.mdf;Integrated Security=True");

change to 改成

    SqlConnection connection = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\MyData.mdf;Integrated Security=True");

or add a method/function that uses the SqlConnentionStringBuilder like this 或添加使用SqlConnentionStringBuilder这样的方法/函数

        public static SqlConnection GetConnection()
    {
        SqlConnectionStringBuilder connectionString = new SqlConnectionStringBuilder();
        connectionString.DataSource = "(localdb)\\MSSQLLocalDB";
        connectionString.AttachDBFilename = "|DataDirectory|\\MyData.mdf";
        connectionString.IntegratedSecurity = true;
        string connectString = connectionString.ConnectionString;

        SqlConnection connection = new SqlConnection(connectString);
        return connection;
    }

and then call the method like this 然后像这样调用方法

SqlConnection connection = GetConnection()

and when you hit the database you should have a try catch finally like this 当您访问数据库时,您应该像这样最终尝试

try
        {
            connection.Open();
            insertCommand.ExecuteNonQuery();
            string selectStatement =
                "SELECT IDENT_CURRENT('TableName') FROM TableName";
            SqlCommand selectCommand = new SqlCommand(selectStatement, connection);
            int vendorID = Convert.ToInt32(selectCommand.ExecuteScalar());
            return vendorID;
        }
        catch (SqlException ex)
        {
            throw ex;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            connection.Close();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM