昨天学习了数据库的连接!
我就来点小发挥,做这么个简易的MSSQL连接测试查询!貌似有BUG…..
晕! 贴点代码 防止忘记了…………………….
PS:昨天的强对流天气差点要了我的命……..
雷击就直接在我旁边了 我吓人啊……..
还好到家了才下雨…………………………
数据库测试软件.rar
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Sql_Soft_ceshi_1
{
public partial class Form1 : Form
{
private SqlCommand command;
private SqlConnection connection;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string username;
string password;
string database;
string address;
string tablename;
address = textBox1.Text;
database = textBox2.Text;
username = textBox3.Text;
password = textBox4.Text;
tablename = textBox5.Text;
if (address == string.Empty)
{
MessageBox.Show(“数据库地址不能为空……”);
}
else
{
if (database == string.Empty)
{
MessageBox.Show(“数据库名不能为空……”);
}
else
{
if (username == string.Empty)
{
MessageBox.Show(“连接用户名不能留空……”);
}
else
{
if (password == string.Empty)
{
MessageBox.Show(“连接密码不能留空……”);
}
else
{
try
{
connection =
new SqlConnection(“Persist Security Info=False;User ID=” + username + “;Password=” + password + “;Initial Catalog=” + database + “;Server=” + address + “;”);
connection.Open();
MessageBox.Show(“数据库连接成功…..”);
command =
new SqlCommand(“select * from” + ” ” + tablename, connection);
Object objRes = command.ExecuteScalar();
MessageBox.Show(“表名查询成功…….”);
MessageBox.Show(“查询结果为 ” +
objRes.ToString());
textBox6.Text = objRes.ToString();
}
catch (SqlException exception)
{
MessageBox.Show(“数据库连接失败……”);
}
catch (Exception exception)
{
MessageBox.Show(“查询语句失败…..\n” +
exception.StackTrace.ToString());
}
finally
{
connection.Close();
MessageBox.Show(“结束程序…….”);
}
}
}
}
}
}
}
}