1
我想從XML文件讀取SQL信息。我得到的SQL字符串康涅狄格州這個錯誤:使用XML的SQL連接
格式的初始化字符串的不符合規範開始於索引0
XML:
<?xml version="1.0" encoding="utf-8" ?>
<xml encoding="UTF8">
<configuration>
<appsettings>
<servername>127.0.0.1</servername>
<username>sa</username>
<password>RandomlyZE6</password>
<database>TelecomNames</database>
</appsettings>
</configuration>
</xml>
我的代碼:
var doc = new XPathDocument(Application.StartupPath + "/DBConn.xml");
var navigator = doc.CreateNavigator();
var serverName = navigator.SelectSingleNode("//appsettings/servername");
// var serverIP = navigator.SelectSingleNode("//appsettings/serverport");
var username = navigator.SelectSingleNode("//appsettings/username");
var password = navigator.SelectSingleNode("//appsettings/password");
var database = navigator.SelectSingleNode("//appsettings/database");
var connection = new SqlConnection(string.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};MultipleActiveResultSets = True;", serverName, database, username, password));
using (SqlConnection sqlConnection = new SqlConnection(connection.ToString())) ;
{
SqlCommand cmd = new SqlCommand("use TelecomNames SELECT xName FROM dbo.Character", connection);
connection.Open();
cmd.ExecuteNonQuery();
SqlDataReader reader = cmd.ExecuteReader();
AutoCompleteStringCollection MyCollection = new AutoCompleteStringCollection();
while (reader.Read())
{
}
connection.Close();
}
錯誤在那條線上
using (SqlConnection sqlConnection = new SqlConnection(connection.ToString()))
感謝
心靈共享XML內容? – Reniuz
完成後,我添加了它 – user3091970
您確實創建了一個'new SqlConnection',然後使用'toString()'創建一個新的(參見'SqlConnection sqlConnection = new SqlConnection(connection.ToString())') - 爲什麼? ?這不能工作! –