22,209
社区成员
发帖
与我相关
我的任务
分享
namespace WinXPConnectionSQL
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string strSQL="";
SqlConnection conn = null ;
SqlCommand cmd=null ;
try
{
strSQL = "Server=111.111.111.111;Database=111;User id=11111;PWD=1111";//本处IP等作了打乱
conn = new SqlConnection(strSQL);
cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
strSQL = "select * from tb_City";
cmd.CommandType=CommandType.Text;
cmd.CommandText = strSQL;
if(conn.State==ConnectionState.Open)
{
//sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
SqlDataReader dr=cmd.ExecuteReader(CommandBehavior.CloseConnection);
dr.Read();
MessageBox.Show(dr[1].ToString() + "/" + dr[2].ToString() + "/" + dr[3].ToString() + "/" + dr[4].ToString());
if (!dr.IsClosed) { dr.Close(); }
}
}
catch (Exception ex)
{
if (conn.State == ConnectionState.Open) { conn.Close(); }
throw ex;
}
}
}
}