NiceGirl13
新成员
- 已加入
- 2015年11月19日
- 留言内容
- 4
- 编程经验
- Beginner
我创建了一个WPF项目,用户可以在其中将Servername插入文本框中,并将福彩12选5走势图保存在app.config中(在运行时中),将完成后从数据库表中获取的数据加载到datagrid中。当我按下福彩12选5走势图按钮时(在文本框中插入服务器名称之后),我收到以下消息:

代码如下:
App.config
MainWindow.xaml.cs

代码如下:
App.config
C#:
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="SQLconnectionString"
connectionString=""
providerName="System.Data.sqlclient"/>
</connectionStrings>
</configuration>
MainWindow.xaml.cs
C#:
namespace CIA
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonConnectServer_Click(object sender, RoutedEventArgs e)
{
try
{
StringBuilder Con = new StringBuilder("Server=");
Con.Append(TextBoxServerName.Text);
Con.Append("; Database=SIT-DVH; Trusted_Connection=True;");
string strCon = Con.ToString();
updateConfigFile(strCon);
SqlConnection db = new SqlConnection();
ConfigurationManager.RefreshSection("connectionStrings");
db.ConnectionString = ConfigurationManager.ConnectionStrings ["SQLconnectionString"].ToString();
// if connect to the server and database is there, load values into the datagrid
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Customer_Information",db);
DataTable data = new DataTable();
sda.Fill(data);
DataGridView_Customer_Information.DataContext = data;
//or change to this:
//DataGridView_Customer_Information.ItemsSource = data.DefaultView;
}
catch (Exception ex)
{
MessageBox.Show("Error: \r\n" + ex);
}
}
public void updateConfigFile(string con)
{
//Updating config file
XmlDocument XmlDoc = new XmlDocument();
//Loading the config file
XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
foreach (XmlElement xElement in XmlDoc.DocumentElement)
{
if (xElement.Name == "connectionStrings")
{
//setting the connectionStrings
xElement.FirstChild.Attributes[2].Value = SQLconnectionString;
}
}
//writing the 福彩12选5走势图字符串 into the config file
XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
}
}
}
Last edited: