一.设置窗口
1.主要包含设置服务器、用户、密码等
这个是子窗体
即设置服务器连接数据库
构造函数里写(Form form1)
子窗体功能;
1.读取ini文件的数据库设置
2.用户根据自己的需求改变设置
3.保存设置进ini文件
4.改变主窗体的SqlConnection—-此处详见
语雀内容
二、完整的子窗体代码
public partial class SqlConnector : DevExpress.XtraEditors.XtraForm
{
private string filepath = "";
public SqlConnector()
{
InitializeComponent();
filepath = Form1.GetDirectoryInfo(3) + "Log\\SqlConfig.ini";
this.txt_Server.Text = INIHelper.Read("SqlConnector", "server", "127.0.0.1", filepath);
this.txt_UseID.Text = INIHelper.Read("SqlConnector", "userID", "root", filepath);
this.txt_Port.Text = INIHelper.Read("SqlConnector", "port", "3306", filepath);
}
public SqlConnector(Form1 form1)
{
InitializeComponent();
filepath = Form1.GetDirectoryInfo(3) + "Log\\SqlConfig.ini";
this.txt_Server.Text = INIHelper.Read("SqlConnector", "server", "127.0.0.1", filepath);
this.txt_UseID.Text = INIHelper.Read("SqlConnector", "userID", "root", filepath);
this.txt_Port.Text = INIHelper.Read("SqlConnector", "port", "3306", filepath);
this.txt_Password.Text = INIHelper.Read("SqlConnector", "password", "mypassword", filepath);
}
//定义委托、事件
public delegate void ChangeSqlConnection(string connStr);
public event ChangeSqlConnection changeSqlConn;
/// <summary>
/// 关闭窗体时重写ini文件且设置父窗体的SqlConnector
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SqlConnector_FormClosed(object sender, FormClosedEventArgs e)
{
if (!string.IsNullOrEmpty(filepath))
{
INIHelper.Write("SqlConnector", "server", txt_Server.Text.ToString(), filepath);
INIHelper.Write("SqlConnector", "userID", txt_UseID.Text.ToString(), filepath);
INIHelper.Write("SqlConnector", "port", txt_Port.Text.ToString(), filepath);
INIHelper.Write("SqlConnector", "password", txt_Password.Text.ToString(), filepath);
string connStr = String.Format("server={0};user id={1}; password={2}; port={3}; database=mysql; pooling=false; charset=utf8", txt_Server.Text.ToString().Trim(), txt_UseID.Text.ToString().Trim(), txt_Password.Text.ToString(),txt_Port.Text.ToString().Trim());
changeSqlConn(connStr);
}
}
}
三、INIHelper—-用来改写ini中某个节点下的key对应的value值
INI文件构造
[section]
key=value
class INIHelper
{
/// <summary>
/// 为INI文件中指定的节点取得字符串
/// </summary>
/// <param name="lpAppName">欲在其中查找关键字的节点名称</param>
/// <param name="lpKeyName">欲获取的项名</param>
/// <param name="lpDefault">指定的项没有找到时返回的默认值</param>
/// <param name="lpReturnedString">指定一个字串缓冲区,长度至少为nSize</param>
/// <param name="nSize">指定装载到lpReturnedString缓冲区的最大字符数量</param>
/// <param name="lpFileName">INI文件完整路径</param>
/// <returns>复制到lpReturnedString缓冲区的字节数量,其中不包括那些NULL中止字符</returns>
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
/// <summary>
/// 修改INI文件中内容
/// </summary>
/// <param name="lpApplicationName">欲在其中写入的节点名称</param>
/// <param name="lpKeyName">欲设置的项名</param>
/// <param name="lpString">要写入的新字符串</param>
/// <param name="lpFileName">INI文件完整路径</param>
/// <returns>非零表示成功,零表示失败</returns>
[DllImport("kernel32")]
private static extern int WritePrivateProfileString(string lpApplicationName, string lpKeyName, string lpString, string lpFileName);
/// <summary>
/// 读取INI文件值
/// </summary>
/// <param name="section">节点名</param>
/// <param name="key">键</param>
/// <param name="def">未取到值时返回的默认值</param>
/// <param name="filePath">INI文件完整路径</param>
/// <returns>读取的值</returns>
public static string Read(string section, string key, string def, string filePath)
{
StringBuilder sb = new StringBuilder(1024);
GetPrivateProfileString(section, key, def, sb, 1024, filePath);
return sb.ToString();
}
/// <summary>
/// 写INI文件值
/// </summary>
/// <param name="section">欲在其中写入的节点名称</param>
/// <param name="key">欲设置的项名</param>
/// <param name="value">要写入的新字符串</param>
/// <param name="filePath">INI文件完整路径</param>
/// <returns>非零表示成功,零表示失败</returns>
public static int Write(string section, string key, string value, string filePath)
{
//CheckPath(filePath);
return WritePrivateProfileString(section, key, value, filePath);
}
/// <summary>
/// 删除节
/// </summary>
/// <param name="section">节点名</param>
/// <param name="filePath">INI文件完整路径</param>
/// <returns>非零表示成功,零表示失败</returns>
public static int DeleteSection(string section, string filePath)
{
return Write(section, null, null, filePath);
}
/// <summary>
/// 删除键的值
/// </summary>
/// <param name="section">节点名</param>
/// <param name="key">键名</param>
/// <param name="filePath">INI文件完整路径</param>
/// <returns>非零表示成功,零表示失败</returns>
public static int DeleteKey(string section, string key, string filePath)
{
return Write(section, key, null, filePath);
}
/// <summary>
/// 检查文件是否存在
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
private static void CheckPath(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath) || !File.Exists(filePath))
{
throw new ArgumentNullException("filePath");
}
}
}
四、主窗体中数据库代码
#region 数据库操作
public MySqlConnection conn;
private DataTable data;
private MySqlDataAdapter da;
private MySqlCommandBuilder cb;
private DataGrid dataGrid;
private void barButtonItem3_ItemClick(object sender, ItemClickEventArgs e)
{
SqlConnector sql = new SqlConnector(this);
sql.changeSqlConn += new SqlConnector.ChangeSqlConnection(SetSqlConnection);
sql.Show(this);
}
/// <summary>
/// 根据INI文件中的设置改变数据库的连接
/// </summary>
private void SetSqlConnection(string connStr)
{
if(conn!=null)
{
conn.Close();
}
try
{
conn = new MySqlConnection(connStr);
conn.Open();
XtraMessageBox.Show("连接数据库成功!");
}
catch(Exception ex)
{
XtraMessageBox.Show("Error connecting to the server:" + ex.Message);
}
}
#endregion