c#配置文件的简单操作
// 配置文件 <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <!-- 配置文件,连接数据库字符串 --> <add key="myconn" value="server=.;database=students; uid=sa;pwd=123456" /> </appSettings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/> </startup> </configuration> // form文件 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; using System.Configuration; using System.Data.SqlClient; namespace myConfig { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void bt_Data_Click(object sender, EventArgs e) { // 获取连接字符串信息 string connstr = ConfigurationManager.AppSettings["myconn"]; using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); string strcmd = "select * from stu;"; DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(strcmd,connstr); da.Fill(ds,"MyTable"); dgv_Data.DataSource = ds.Tables["MyTable"]; } } } }