19,504
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DeviceAppText
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=192.168.1.100;uid=sa;pwd=1;database=Northwind");
try
{
//出现异常无法连接数据库
SqlCommand com =new SqlCommand("select * from Orders",con);
com.CommandType = CommandType.Text;
con.Open();
SqlDataAdapter dr = new SqlDataAdapter(com);
DataSet ds = new DataSet();
dr.Fill(ds);
//int i = Convert.ToInt32(com.Parameters["@Return"].Direction);
//if (i == 0)
//{
// MessageBox.Show("用户名或密码错误");
//}
//else if (i == 1)
//{
// frmGnxzq objGnxzq = new frmGnxzq();
// this.Hide();
// objGnxzq.Show();
//}
MessageBox.Show(ds.Tables[0].Rows[0][0].ToString());
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
//closesqlconnection();
con.Close();
con.Dispose();
}
}
}
}
SqlConnection con = new SqlConnection("server=192.168.15.40;uid=sa;pwd=123456;database=coldSMS");
try
{
con.Open(); //出现异常无法连接数据库
SqlCommand com = con.CreateCommand();
com.CommandType = CommandType.StoredProcedure;
com.CommandText = "logg";
com.Parameters.Add(new SqlParameter("@a", SqlDbType.Int, 4));
com.Parameters["@a"].Value = logname;
com.Parameters.Add(new SqlParameter("@b", SqlDbType.Int, 4));
com.Parameters["@b"].Value = logpwd;
com.Parameters.Add(new SqlParameter("@Return", SqlDbType.Int, 4));
com.Parameters["@Return"].Direction = ParameterDirection.ReturnValue;
com.ExecuteNonQuery();
int i = Convert.ToInt32(com.Parameters["@Return"].Direction);
if (i == 0)
{
MessageBox.Show("用户名或密码错误");
this.txtZh.Text = "";
this.txtMm.Text = "";
this.txtZh.Focus();
}
else if (i == 1)
{
frmGnxzq objGnxzq = new frmGnxzq();
this.Hide();
objGnxzq.Show();
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
//closesqlconnection();
con.Close();
con.Dispose();
}
}