diff --git a/Login.xaml.cs b/Login.xaml.cs index 3ab01ce..8e16bf1 100644 --- a/Login.xaml.cs +++ b/Login.xaml.cs @@ -14,6 +14,7 @@ using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.Runtime.InteropServices; +using System.Data.SqlClient; namespace formula_manage { @@ -21,36 +22,13 @@ namespace formula_manage /// Login.xaml 的交互逻辑 /// public partial class Login : Window - { - [DllImport("user32.dll", SetLastError = true)] - static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo); - public static void PressKey(Keys key, bool up) - { - const int KEYEVENTF_EXTENDEDKEY = 0x1; - const int KEYEVENTF_KEYUP = 0x2; - if (up) - { - keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0); - } - else - { - keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0); - } - } - + { public Login() { WindowStartupLocation = WindowStartupLocation.CenterScreen; InitializeComponent(); } - private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e) - { - Regex re = new Regex("^[1-9]+[0-9]*$"); - - e.Handled = !re.IsMatch(e.Text); - } - private void Button_Click(object sender, RoutedEventArgs e) //退出按钮事件 { this.Close(); @@ -71,20 +49,49 @@ namespace formula_manage }*/ } - private void SearchBox_OnKeyDownd(object sender, System.Windows.Input.KeyEventArgs e) + private void SearchBox_OnKeyDownd(object sender, System.Windows.Input.KeyEventArgs e) //回车跳转功能 { if (e.Key == Key.Enter) { //SendKeys.SendWait("{Tab}"); - PressKey(Keys.Tab, false); - PressKey(Keys.Tab, true); + UserClass.PressKey.PressKeys(Keys.Tab, false); + UserClass.PressKey.PressKeys(Keys.Tab, true); } } - private void Window_Loaded(object sender, RoutedEventArgs e) + public string INIPath = Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "formula.ini"; + + + string TEXT_SQLIP; + string TEXT_SQLNAME; + string TEXT_SQMOD; + string TEXT_SQLUSER; + string TEXT_SQLPASWOR; + string Connstr_SC; + int i; + private void Window_Loaded(object sender, RoutedEventArgs e) { - PressKey(Keys.Tab, false); - PressKey(Keys.Tab, true); + UserClass.IniFile.IniFiles Configini = new UserClass.IniFile.IniFiles(INIPath); + TEXT_SQLIP = Configini.IniReadvalue("SQL_SERVER", "SQL1"); //读配置文件 + TEXT_SQLNAME = Configini.IniReadvalue("SQL_SERVER", "SQL2"); + TEXT_SQMOD = Configini.IniReadvalue("SQL_SERVER", "SQL3"); + TEXT_SQLUSER = Configini.IniReadvalue("SQL_SERVER", "SQL4"); + TEXT_SQLPASWOR = Configini.IniReadvalue("SQL_SERVER", "SQL5"); + int.TryParse(Configini.IniReadvalue("SQL_SERVER", "SQL3"), out i); //读数据库状态配置文件 + if (i == 0) + { + Connstr_SC = "server=" + TEXT_SQLIP + ";database=" + TEXT_SQLNAME + ";Trusted_Connection=SSPI"; + } + else + { + Connstr_SC = "server=" + TEXT_SQLIP + ";database=" + TEXT_SQLNAME + ";User ID=" + TEXT_SQLUSER + ";Password=" + TEXT_SQLPASWOR; + } + SqlConnection conn_DBC = new SqlConnection(Connstr_SC); + conn_DBC.Open(); + conn_DBC.Close(); + + UserClass.PressKey.PressKeys(Keys.Tab, false); + UserClass.PressKey.PressKeys(Keys.Tab, true); } } } diff --git a/UserClass/IniFile.cs b/UserClass/IniFile.cs new file mode 100644 index 0000000..08ccb63 --- /dev/null +++ b/UserClass/IniFile.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace formula_manage.UserClass +{ + internal class IniFile + { + public class IniFiles + { + public string path; + [DllImport("kernel32")] //返回0表示失败,非0为成功 + private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); + [DllImport("kernel32")] //返回取得字符串缓冲区的长度 + private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); + /// + /// 保存ini文件的路径 + /// 调用示例:var ini = IniFiles("C:\file.ini"); + /// + /// + public IniFiles(string iniPath) + { + this.path = iniPath; + } + /// + /// 写Ini文件 + /// 调用示例:ini.IniWritevalue("Server","name","localhost"); + /// + /// [缓冲区] + /// 键 + /// 值 + public void IniWritevalue(string Section, string Key, string value) + { + WritePrivateProfileString(Section, Key, value, this.path); + } + /// + /// 读Ini文件 + /// 调用示例:ini.IniWritevalue("Server","name"); + /// + /// [缓冲区] + /// 键 + /// + public string IniReadvalue(string Section, string Key) + { + StringBuilder temp = new StringBuilder(255); + + int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path); + return temp.ToString(); + } + + } + } +} diff --git a/UserClass/PressKey.cs b/UserClass/PressKey.cs new file mode 100644 index 0000000..971cc79 --- /dev/null +++ b/UserClass/PressKey.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace formula_manage.UserClass +{ + internal class PressKey + { + [DllImport("user32.dll", SetLastError = true)] + static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo); + public static void PressKeys(Keys key, bool up) + { + const int KEYEVENTF_EXTENDEDKEY = 0x1; + const int KEYEVENTF_KEYUP = 0x2; + if (up) + { + keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0); + } + else + { + keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0); + } + } + } +} diff --git a/Windows/Sql.xaml.cs b/Windows/Sql.xaml.cs index e5539e0..873022b 100644 --- a/Windows/Sql.xaml.cs +++ b/Windows/Sql.xaml.cs @@ -17,6 +17,7 @@ using System.Runtime.InteropServices; using System.ComponentModel; using System.Data.SqlTypes; + namespace formula_manage.Windows { /// @@ -24,49 +25,6 @@ namespace formula_manage.Windows /// public partial class Sql : Window { - public class IniFiles - { - public string path; - [DllImport("kernel32")] //返回0表示失败,非0为成功 - private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); - [DllImport("kernel32")] //返回取得字符串缓冲区的长度 - private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); - /// - /// 保存ini文件的路径 - /// 调用示例:var ini = IniFiles("C:\file.ini"); - /// - /// - public IniFiles(string iniPath) - { - this.path = iniPath; - } - /// - /// 写Ini文件 - /// 调用示例:ini.IniWritevalue("Server","name","localhost"); - /// - /// [缓冲区] - /// 键 - /// 值 - public void IniWritevalue(string Section, string Key, string value) - { - WritePrivateProfileString(Section, Key, value, this.path); - } - /// - /// 读Ini文件 - /// 调用示例:ini.IniWritevalue("Server","name"); - /// - /// [缓冲区] - /// 键 - /// - public string IniReadvalue(string Section, string Key) - { - StringBuilder temp = new StringBuilder(255); - - int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path); - return temp.ToString(); - } - - } public Sql() { WindowStartupLocation = WindowStartupLocation.CenterScreen; @@ -83,7 +41,7 @@ namespace formula_manage.Windows private void Window_SQL(object sender, RoutedEventArgs e) { - IniFiles Configini = new IniFiles(INIPath); + UserClass.IniFile.IniFiles Configini = new UserClass.IniFile.IniFiles(INIPath); TEXT_SQLIP.Text = Configini.IniReadvalue("SQL_SERVER", "SQL1"); //读配置文件 TEXT_SQLNAME.Text = Configini.IniReadvalue("SQL_SERVER", "SQL2"); @@ -122,7 +80,7 @@ namespace formula_manage.Windows SQL2 = TEXT_SQLINK.IsChecked.GetValueOrDefault(); //数据库对象2是否生效 - IniFiles Configini = new IniFiles(INIPath); //配置文件 + UserClass.IniFile.IniFiles Configini = new UserClass.IniFile.IniFiles(INIPath); //配置文件 Configini.IniWritevalue("SQL_SERVER", "SQL1", TEXT_SQLIP.Text); //保存的配置 Configini.IniWritevalue("SQL_SERVER", "SQL2", TEXT_SQLNAME.Text); Configini.IniWritevalue("SQL_SERVER", "SQL3", SQL_MOD.ToString()); diff --git a/formula_manage.csproj b/formula_manage.csproj index aabbaa4..3d57e40 100644 --- a/formula_manage.csproj +++ b/formula_manage.csproj @@ -80,6 +80,8 @@ Login.xaml + + Sql.xaml