110,534
社区成员
发帖
与我相关
我的任务
分享
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 sql_test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string strConnection = "Server= (local);";
strConnection += "initial catalog= test;";
strConnection += "user id= tongfang-PC\tongfang;";
strConnection += "password=******;";
strConnection += "Connect Timeout=5";
bool CanConnectDB = false;
using (SqlConnection objConnection = new SqlConnection(strConnection))
{
try
{
objConnection.Open();
CanConnectDB = true;
objConnection.Close();
}
catch { }
if (CanConnectDB) MessageBox.Show("数据库连接成功!", "提示:");
else MessageBox.Show("数据库连接失败!", "提示:");
}
}
}
}
#if DEBUG
abc();
#else
try
{
abc();
}
catch (Exception ex)
{
........
}
#endif
或者直接在 AppDomain.CurrentDomain.UnhandledException 以及 应用程序、窗体、页面、站点、Global.asax 等等层面去全局捕获异常。其实越是在低级的代码层面写 try......catch....... 代码,越是说明你是自学编程的。