代码拉取完成,页面将自动刷新
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Nikita.Assist.Logger
{
public partial class FrmLoggerInit : Form
{
public FrmLoggerInit()
{
InitializeComponent();
}
private string strDBName;
private void btnCon_Click(object sender, EventArgs e)
{
if (cboLogType.Text.Trim() == string.Empty)
{
MessageBox.Show("请选择初始化日志类型");
return;
}
FrmDbLogin login = new FrmDbLogin(cboLogType.Text);
if (login.ShowDialog() == DialogResult.OK)
{
txtConnection.Text = login.strConn;
strDBName = login.DBName;
txtInitInfo.Text = GetCreateLogTableSql(cboLogType.Text);
}
else
{
txtInitInfo.Text = txtConnection.Text = string.Empty;
}
}
private bool InitLogTable(string strExistsTbSql, string strTbName, string strDbName)
{
bool flag = true;
string strConn = DESEncryptHelper.Decrypt(txtConnection.Text.Trim(), "test332211");
IDBHelper dbHelper = LoggerHelper.GetDBHelper(this.cboLogType.Text, strConn);
if (dbHelper != null)
{
dbHelper.CreateCommand(strExistsTbSql);
int intResult = dbHelper.ExecuteQuery().Rows.Count;
;
if (intResult > 0)
{
if (MessageBox.Show("已经存在日志记录表" + strTbName + ",是否删除重新建立?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
string strDropTable = "DROP TABLE " + strTbName + "";
dbHelper.CreateCommand(strDropTable);
try
{
dbHelper.ExecuteNonQuery();
}
catch (Exception)
{
MessageBox.Show("删除失败");
flag = false;
}
flag = DoCreateTable(dbHelper);
}
}
else
{
flag = DoCreateTable(dbHelper);
}
}
return flag;
}
private bool DoCreateTable(IDBHelper dbHelper)
{
bool flag = true;
try
{
string sql = GetCreateLogTableSql(this.cboLogType.Text);
if (sql == string.Empty)
{
MessageBox.Show("未能生成建表语句");
flag = false;
}
else
{
dbHelper.CreateCommand(sql);
DataTable dt = dbHelper.ExecuteQuery();
}
}
catch (Exception)
{
MessageBox.Show("初始化失败");
flag = false;
}
return flag;
}
private void FrmLoggerInit_Load(object sender, EventArgs e)
{
this.cboLogType.SelectedIndex = 0;
}
private void cboLogType_SelectedIndexChanged(object sender, EventArgs e)
{
if (cboLogType.Text == "sqlite")
{
txtConnection.Visible = btnCon.Visible = false;
}
else
{
txtConnection.Visible = btnCon.Visible = true;
}
}
private void btnOK_Click(object sender, EventArgs e)
{
if (cboLogType.Text.Trim().ToLower() != "sqlite")
{
if (!CheckInit())
return;
string strExistsTbSql = string.Empty;
string strTbName = string.Empty;
string strDbName = strDBName;
if (cboLogType.Text.Trim().ToLower() == "mysql")
{
strExistsTbSql = @"SELECT 1 FROM information_schema.TABLES WHERE table_name = 'mysqllog' AND TABLE_SCHEMA = '" + strDbName + "'";
strTbName = "mysqllog";
}
else if (cboLogType.Text.Trim().ToLower() == "sqlserver")
{
/*判断表是否存在*/
strExistsTbSql = " SELECT 1FROM sysobjects WHERE id = OBJECT_ID(N'[SqlserverLog]') AND OBJECTPROPERTY(id,N'IsUserTable') = 1 ";
strTbName = "SqlserverLog";
}
bool flag = InitLogTable(strExistsTbSql, strTbName, strDBName);
if (!flag)
{
return;
}
}
CreateIniFile();
this.DialogResult = DialogResult.OK;
StaticInfoHelper.LogType = cboLogType.Text.Trim().ToLower() + "log";
StaticInfoHelper.ConnString = txtConnection.Text.Trim();
}
/// <summary>
/// 创建本地配置文件
/// </summary>
private void CreateIniFile()
{
string filePath = Application.StartupPath + "\\" + "Log.ini";
if (!File.Exists(filePath))
{
File.Create(filePath).Close();
}
else
{
File.Delete(filePath);
File.Create(filePath).Close();
}
if (cboLogType.Text.Trim().ToLower() == "sqlite")
{
INIOperationHelper.INIWriteValue(filePath, "LogConnection", "Connection", DESEncryptHelper.Encrypt("LogLocalDB", "test332211"));
}
else
{
INIOperationHelper.INIWriteValue(filePath, "LogConnection", "Connection", txtConnection.Text.Trim());
}
INIOperationHelper.INIWriteValue(filePath, "LogType", "Type", cboLogType.Text.Trim().ToLower() + "log");
}
//string value = INIOperationHelper.INIGetStringValue(file, "LogConnection", "Connection", null);
//string value1 = INIOperationHelper.INIGetStringValue(file, "LogType", "Type", null);
private bool CheckInit()
{
bool Initflag = true;
if (cboLogType.Text.Trim() == string.Empty)
{
MessageBox.Show("请选择初始化数据库类型");
cboLogType.Select();
Initflag = false;
}
if (txtConnection.Text.Trim() == string.Empty || txtInitInfo.Text.Trim() == string.Empty)
{
MessageBox.Show("请选添加连接");
Initflag = false;
}
return Initflag;
}
private string GetCreateLogTableSql(string strType)
{
string strSql = string.Empty;
if (strType.ToLower() == "mysql")
{
strSql = @"
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for mysqllog
-- ----------------------------
DROP TABLE IF EXISTS `mysqllog`;
CREATE TABLE `mysqllog` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Date` datetime DEFAULT NULL,
`Level` tinyint(4) DEFAULT NULL,
`Logger` varchar(50) DEFAULT NULL,
`Message` varchar(8000) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
";
}
else if (strType.ToLower() == "sqlserver")
{
strSql = @"
CREATE TABLE [dbo].[SqlserverLog] (
[id] int NOT NULL IDENTITY(1,1) ,
[Date] datetime NULL ,
[Level] tinyint NULL ,
[Logger] varchar(50) NULL ,
[Message] varchar(8000) NULL ,
CONSTRAINT [PK_SqlserverLog] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]";
}
return strSql;
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。