标签:using top tor tsql base source lda isp space
当前获取的连接是直接连接mdf文件,需要SQLEXPRESS服务运行,mdf文件位置在当前目录文件夹下
static string conString = "Data Source=.\\SQLEXPRESS;AttachDbFilename="+ System.IO.Directory.GetCurrentDirectory() + "\\ManageSystem.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
根据用户ID查找用户身份信息和密码
string commend = "select * from UserInfo where UserId=2";
SqlDataReader sqlDr = Utils.getDataReader(commend);
try
{
if (sqlDr.Read()) //返回json数据,根据字段查找自己需要的信息
{
string userididenity1 = (String)sqlDr["UserIdentity"];
string password = (String)sqlDr["UserPwd"];
}
else
{
MessageBox.Show("用户名不存在");
}
sqlDr.Close();
}
catch
{
MessageBox.Show("数据库连接异常,请重新检查连接,检查你的SQL服务是否为SQLEXPRESS");
}
string str = "insert bookInfo(bookId,bookName,bookAuthor,bookPub,bookContent,bookQuantity) values(" + textBox1.Text + ",‘" + textBox2.Text + "‘,‘" + textBox3.Text + "‘,‘" + textBox4.Text + "‘,‘" + textBox5.Text+" ‘,‘" + Quantity.Text + "‘)";
if (Utils.ExecuteNoQuery(str) != -1)
{
MessageBox.Show("添加成功");
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace ManageSystem
{
static class Utils
{
//static string conString ;
//static SqlConnection sqlcon;
/*连接mdf*/
static string conString = "Data Source=.\\SQLEXPRESS;AttachDbFilename="+ System.IO.Directory.GetCurrentDirectory() + "\\ManageSystem.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
static SqlConnection sqlcon = new SqlConnection(conString);
public static SqlConnection getSqlCon()
{
if (sqlcon.State != System.Data.ConnectionState.Open)
{
string dataDir = AppDomain.CurrentDomain.BaseDirectory;
sqlcon.Open();
//if (sqlCon.State == System.Data.ConnectionState.Open)
// return true;
//else
// return false;
return sqlcon;
}
return sqlcon;
}
public static void CreateConnection()
{
if (sqlcon.State != System.Data.ConnectionState.Open)
{
sqlcon.Open();
}
else
{
CloseConnection();
sqlcon.Open();
}
}
public static void CloseConnection()
{
sqlcon.Close();
}
public static SqlCommand Querry(string strsql)
{
try
{
CreateConnection();
SqlCommand sqlcmd = new SqlCommand(strsql, sqlcon);
sqlcmd.CommandText = strsql;
return sqlcmd;
}
catch
{
return null;
}
}
public static SqlDataReader getDataReader(string strsql)
{
try
{
CreateConnection();
SqlCommand sqlcmd = new SqlCommand(strsql, sqlcon);
sqlcmd.CommandText = strsql;
SqlDataReader sqlDr = sqlcmd.ExecuteReader();
return sqlDr;
}
catch
{
return null;
}
}
//执行Insert/update/delete,不带参数
public static int ExecuteNoQuery(string strsql)
{
int i;
try
{
CreateConnection();
SqlCommand sqlcmd = new SqlCommand(strsql, sqlcon);
i = sqlcmd.ExecuteNonQuery();
sqlcon.Close();
return i;
}
catch
{
return -1;
}
}
//执行Insert/update/delete,带参数
public static int ExecuteNoQuery(string strsql, params SqlParameter[] param)
{
int i;
try
{
CreateConnection();
SqlCommand sqlcmd = new SqlCommand(strsql, sqlcon);
//sqlcmd.Parameters.Add(param);
foreach (SqlParameter par in param) //遍历数组将参数对象添加到操作命令中
{
sqlcmd.Parameters.Add(par);
}
i = sqlcmd.ExecuteNonQuery();
return i;
}
catch
{
return -1;
}
}
public static DataSet GetDataSet(string strsql)
{
CreateConnection();
SqlDataAdapter sda = new SqlDataAdapter(strsql, sqlcon);
DataSet ds = new DataSet();
try
{
sda.Fill(ds);
return ds;
}
catch
{
return null;
}
}
public static DataSet GetDataSet(string strsql, params SqlParameter[] param)
{
CreateConnection();
SqlDataAdapter sda = new SqlDataAdapter(strsql, sqlcon);
DataSet ds = new DataSet();
foreach (SqlParameter par in param)
{
sda.SelectCommand.Parameters.Add(par);
}
try
{
sda.Fill(ds);
return ds;
}
catch
{
return null;
}
}
public static DataTable GetTable(string strsql)
{
try
{
CreateConnection();
SqlDataAdapter sda = new SqlDataAdapter(strsql, sqlcon);
DataSet ds = new DataSet();
sda.Fill(ds, "temp");
sqlcon.Close();
return ds.Tables["temp"];
}
catch
{
return null;
}
}
public static DataTable GetTable(string strsql, params SqlParameter[] param)
{
try
{
CreateConnection();
string str = strsql;
SqlDataAdapter sda = new SqlDataAdapter(strsql, sqlcon);
foreach (SqlParameter par in param)
{
sda.SelectCommand.Parameters.Add(par);
}
DataSet ds = new DataSet();
sda.Fill(ds, "temp");
sqlcon.Close();
return ds.Tables["temp"];
}
catch
{
return null;
}
}
}
}
标签:using top tor tsql base source lda isp space
原文地址:https://www.cnblogs.com/aqyl/p/11254323.html