【实例源码】
朋友们好,该功能在测试中突然发现,通过Windows身份验证时,服务器IP与数据库值为空的时候,测试连接也会提示连接成功,这个BUG请大家给予帮助,谢谢!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace ConData
{
public partial class ConnectionData : Form
{
public ConnectionData()
{
InitializeComponent();
}
private void btnSQLOK_Click(object sender, EventArgs e)
{
string strcon = string.Format("server={0};database={1};uid={2};pwd={3};", this.txtIP.Text.Trim(), txtDataBase.Text.Trim(), txtUser.Text.Trim(), txtPwd.Text.Trim());
try
{
using (SqlConnection con = new SqlConnection(strcon))
{
con.Open();
MessageBox.Show("测试成功!");
con.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnWindowsOK_Click(object sender, EventArgs e)
{
string strCon = string.Format("server={0};database={1};Trusted_Connection=SSPI", txtWindowsIP.Text.Trim(), txtWindowsDB.Text.Trim());
try
{
using (SqlConnection con = new SqlConnection(strCon))
{
con.Open();
MessageBox.Show("测试成功!");
con.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
评论