using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace conn
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
btn_close.Enabled = false;
btn_r.Enabled = false;
}
private SqlConnection con;
private SqlDataReader reader;
private SqlCommand cmd = new SqlCommand();
private void button1_Click(object sender, EventArgs e)
{
conn_q();//1.....>>>1;
}
private void btn_close_Click(object sender, EventArgs e)
{
con.Close();
btn_close.Enabled = false;
btn_r.Enabled = false;
btn_open.Enabled = true;
txt_name.Enabled = false;
}
private void btn_fclose_Click(object sender, EventArgs e)
{
if(con.State!=ConnectionState.Closed)
{
con.Close();
MessageBox.Show("数据连接已经关闭,现在可以安全关闭软件。","数据关闭提示");
}
Application.Exit();
}
private void btn_r_Click(object sender, EventArgs e)
{
if (btn_r.Text == "查询")
{
btn_r.Text = "重新查询";
conn_t();//2.......>>1;
}
else
{
if (reader != null)
{
reader.Close();
}
con.Close();
conn_q();//1......>>>2;
conn_t();//2.......>>2;
}
}
private void btn_next_Click(object sender, EventArgs e)
{
conn_r();//3.....>>>1;
}
///<summery>
///1....
///</summery>
private void conn_q()
{
string strcon;
try
{
if (txtDatabase.Text == "" && txtDatasource.Text == "" && txtLoginname.Text == "" && txtPassword.Text == "")
MessageBox.Show("有重要数据未填写,请确定填写完整!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
else
{
con = new SqlConnection();
strcon = "data source=" + this.txtDatasource.Text.Trim();
strcon += ";user id=" + this.txtLoginname.Text.Trim();
strcon += ";password=" + this.txtPassword.Text.Trim();
strcon += ";initial catalog=" + this.txtDatabase.Text.Trim();
con.ConnectionString = strcon;
//con.ConnectionString = "data source = (local);initial catalog = Address; user id = sa;password =sa";
con.Open();
//MessageBox.Show("数据库连接成功");
btn_open.Enabled = false;
btn_r.Enabled = true;
btn_close.Enabled = true;
txt_name.Enabled = true;
}
}
catch (SqlException ex)
{
MessageBox.Show("数据返回失败。原因:" + ex.Message);
}
}
///<summery>
///2....
///</summery>
private void conn_t()
{
if (txt_name.Text == "")
{
string strsql = "select * from 联系人";
= strsql;
}
else
{
string strsql = "select name,number,number2 from 联系人 where name='" + txt_name.Text.Trim() + "'";
= strsql;
}
cmd.Connection = con;
try
{
reader = cmd.ExecuteReader();
conn_r();//3.....>>>2;
}
catch (SqlException ex)
{
MessageBox.Show("查询失败,原因" + ex.Message, "提示");
}
}
///<summery>
///3....
///</summery>
private void conn_r()
{
if (reader.Read())
{
this.txt_r_name.Text = reader[0].ToString();
this.txt_r_number.Text = reader[1].ToString();
this.txt_r_number2.Text = reader[2].ToString();
}
}
}
}