private ConnClass.SqlConn sql;
private ConnClass.SqlConn sql2 = new ConnClass.SqlConn();
private string connectionString;
private bool bl = false;
public static bool visable = false;
private void Form1_Load(object sender, EventArgs e)
{
if (!File.Exists("connection.txt"))
{
connectionString = "data source=.;initial catalog=card;integrated security=sspi";
sql = new ConnClass.SqlConn(connectionString);
writeConnectionString(connectionString);
}
else
{
sql = new ConnClass.SqlConn(sql2.readerConnectionString());
}
this.cbx_serviecName.Items.Add(".");
this.cbx_serviecName.Items.Add("(local)");
this.cbx_serviecName.Items.Add(Environment.MachineName.ToString());
this.cbx_serviecName.SelectedIndex = 0;
// System.Net.Dns.GetHostName().ToString();//System.Environment.MachineName.ToString();获取计算机名
Height = 235;
getUerName();
getDataBase();
}
private void writeConnectionString(string connStr)
{
FileStream fs = new FileStream("connection.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
try
{
sw.Write(connectionString);
}
catch { }
finally
{
sw.Close();
}
}
private void getDataBase()
{
string m_sDBName = "select name from master..sysdatabases";
if (sql.dataSet(m_sDBName, "master..sysdatabases") != null)
{
int n = sql.dataSet(m_sDBName, "master..sysdatabases").Tables[0].Rows.Count;
for (int i = 0; i < n; i++)
{
this.cbx_DBName.Items.Add(sql.dataSet(m_sDBName, "master..sysdatabases").Tables[0].Rows[i][0].ToString());
}
}
this.cbx_DBName.SelectedIndex = 0;
}
//########登录的存储过程调用###############
private void btn_login_Click(object sender, EventArgs e)
{
string name = this.cbx_uerName.SelectedItem.ToString();
string password = this.txt_passWord.Text.Trim();
SqlParameter[] parameter = new SqlParameter[2];
parameter[0] = new SqlParameter("@name", SqlDbType.VarChar, 10);
parameter[0].Value = name;
parameter[1] = new SqlParameter("@passwd", SqlDbType.VarChar, 20);
parameter[1].Direction = ParameterDirection.Output;
sql.isExistProcudure("proLogin", parameter);
if (parameter[1].Value.Equals(password))
{
visable = true;
this.Close();
}
else
{
MessageBox.Show("密码错误!!!", "系统登录失败");
this.txt_passWord.Text = "";
this.txt_passWord.Focus();
}
}
private void btn_testConn_Click(object sender, EventArgs e)
{
string servieceName= this.cbx_serviecName.SelectedItem.ToString();
string databaseName = this.cbx_DBName.SelectedItem.ToString();
string dbUerName= this.txt_DBUserName.Text.Trim();
string dbPwd = this.txt_DBPwd.Text.Trim();
try
{
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
if (this.cbx_DBName.SelectedItem.ToString() == "card")
{
connectionString = "Data Source=" + servieceName + ";Initial Catalog=" + databaseName + ";Integrated Security=True;uid=" + dbUerName + ";pwd=" + dbPwd + ";";
if (conn.State == ConnectionState.Open)
{
MessageBox.Show("连接成功!!!", "信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information );
if (this.cbx_windows.Checked)
{
this.writeConnectionString("data source=.;initial catalog=bearmanage;integrated security=sspi");
}
if (this.cbx_SQLServer.Checked)
{
this.writeConnectionString(connectionString);
}
conn.Close();
this.txt_DBUserName.Text = "";
this.txt_DBPwd.Text = "";
this.cbx_SQLServer.Checked = false;
this.cbx_serviecName.Enabled = false;
this.txt_DBUserName.Enabled = false;
this.txt_DBPwd.Enabled = false;
}
}
else
{
MessageBox.Show("数据库错误!!!", "错误信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch
{
MessageBox.Show("测试连接失败,请重新输入!!!", "信息提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
this.cbx_uerName.Items.Clear();
this.txt_DBUserName.Text = "";
this.txt_DBPwd.Text = "";
}
finally
{
}
}
数据库、服务器设置
最新推荐文章于 2023-02-05 12:32:54 发布