我试图在visual studio中的表格中插入一个条目,但在我运行我的代码然后尝试查看该表后,我收到此错误消息,
无法导入此数据库。它是不受支持的SQL Server版本或不受支持的数据库兼容性。
以下是尝试插入的代码,
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();
}
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>
在MyData.mdf的属性中,Build Action设置为Content,Copy to Output Directory设置为Copy as new。
在MyDataDataSet.xsd的属性中,Build Action设置为None,Copy to Output Directory设置为Do not copy。
修改
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();
}
}
答案 0 :(得分:1)
在这里
SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MyData.mdf;Integrated Security=True");
更改为
SqlConnection connection = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\MyData.mdf;Integrated Security=True");
或添加使用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;
}
然后调用这样的方法
SqlConnection connection = GetConnection()
当你点击数据库时,你应该像这样尝试捕捉
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();
}
答案 1 :(得分:0)
在服务器资源管理器部分查看本地数据库的属性并检查连接字符串属性,如下所示 - "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\DELL\source\repos\WindowsFormsApp2\WindowsFormsApp2\CarDb.mdf;Integrated Security=True"
将其复制并粘贴到 program.cs 的连接字符串中