/// <summary> /// 通过分页存储过程绑定数据 /// </summary> /// <param name="tablename">表、视图名</param> /// <param name="fieldkey">关键字</param> /// <param name="currentpage">当前页数</param> /// <param name="pagesize">每页的数量</param> /// <param name="fieldshow">显示的字段</param> /// <param name="fieldorder">排序方式</param> /// <param name="where">条件</param> /// <param name="recordcount">总计页数</param> /// <returns></returns> public static DataSet GetGriview(string tablename, string fieldkey, int currentpage, int pagesize, string fieldshow, string fieldorder, string where, ref int recordcount) { SqlConnection conn = ConnectionManger.GetConnection(); conn.Open(); SqlCommand cmd = new SqlCommand("pageination", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@tbname", SqlDbType.NVarChar, 50).Value = tablename; cmd.Parameters.Add("@FieldKey", SqlDbType.NVarChar, 50).Value = fieldkey; cmd.Parameters.Add("@PageCurrent", SqlDbType.Int).Value = currentpage; cmd.Parameters.Add("@PageSize", SqlDbType.Int).Value = pagesize; cmd.Parameters.Add("@FieldShow", SqlDbType.NVarChar, 1000).Value = fieldshow; cmd.Parameters.Add("@FieldOrder", SqlDbType.NVarChar, 50).Value = fieldorder; cmd.Parameters.Add("@Where", SqlDbType.NVarChar, 1000).Value = where; cmd.Parameters.Add("@RecordCount", SqlDbType.Int).Direction = ParameterDirection.Output; cmd.Parameters.Add("@PageCount", SqlDbType.Int).Direction = ParameterDirection.Output; DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); recordcount = (int)cmd.Parameters["@RecordCount"].Value; //pagecount = cmd.Parameters["@PageCount"].Value.ToString(); conn.Close(); return(ds); }
//查询操作,判断是否存在 public static bool IsPriExist(string str) { SqlConnection conn = ConnectionManger.GetConnection(); conn.Open(); SqlCommand cmd = new SqlCommand(str, conn); SqlDataReader reader; try { reader = cmd.ExecuteReader(); if (reader.Read()) { reader.Dispose(); return(true); } else { return(false); } } catch { return(false); } finally { cmd.Dispose(); } }
/// <summary> /// 带参数绑定数据 /// </summary> /// <param name="str">存储过程</param> /// <returns></returns> public static DataSet GetDataSet(string str, CommandType cmdtype, SqlParameter[] cmdParm) { SqlConnection conn = ConnectionManger.GetConnection(); DataSet ds = new DataSet(); try { SqlCommand cmd = new SqlCommand(str, conn); cmd.CommandText = str; cmd.CommandType = cmdtype; //PrepareCommand(cmd, conn, null, cmdtype, str, parm); SqlDataAdapter da = new SqlDataAdapter(cmd); foreach (SqlParameter parm in cmdParm) { cmd.Parameters.Add(parm); } da.Fill(ds); return(ds); } catch { conn.Close(); throw; } }
/// <summary> /// 执行一条返回第一条记录第一列的SqlCommand命令,通过专用的连接字符串。 /// 使用参数数组提供参数 /// </summary> /// <remarks> /// 使用示例: /// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24)); /// </remarks> /// <param name="connectionString">一个有效的数据库连接字符串</param> /// <param name="commandType">SqlCommand命令类型 (存储过程, T-SQL语句, 等等。)</param> /// <param name="commandText">存储过程的名字或者 T-SQL 语句</param> /// <param name="commandParameters">以数组形式提供SqlCommand命令中用到的参数列表</param> /// <returns>返回一个object类型的数据,可以通过 Convert.To{Type}方法转换类型</returns> public static object ExecuteScalar(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); using (SqlConnection connection = ConnectionManger.GetConnection()) { PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); object val = cmd.ExecuteScalar(); cmd.Parameters.Clear(); return(val); } }
/// <summary> ///执行一个不需要返回值的SqlCommand命令,通过指定专用的连接字符串。 /// 使用参数数组形式提供参数列表 /// </summary> /// <remarks> /// 使用示例: /// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24)); /// </remarks> /// <param name="connectionString">一个有效的数据库连接字符串</param> /// <param name="commandType">SqlCommand命令类型 (存储过程, T-SQL语句, 等等。)</param> /// <param name="commandText">存储过程的名字或者 T-SQL 语句</param> /// <param name="commandParameters">以数组形式提供SqlCommand命令中用到的参数列表</param> /// <returns>返回一个数值表示此SqlCommand命令执行后影响的行数</returns> public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); using (SqlConnection conn = ConnectionManger.GetConnection()) { //通过PrePareCommand方法将参数逐个加入到SqlCommand的参数集合中 PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); int val = cmd.ExecuteNonQuery(); //清空SqlCommand中的参数列表 cmd.Parameters.Clear(); return(val); } }
public static DataSet DataSetBind(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); using (SqlConnection connection = ConnectionManger.GetConnection()) { DataSet ds = new DataSet(); PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); //object val = cmd.ExecuteScalar(); cmd.Parameters.Clear(); return(ds); } }
//public void readxml() //{ // XmlDocument xmlDoc = new XmlDocument(); // xmlDoc.Load("han.xml"); // XmlNodeList nodeList = xmlDoc.SelectSingleNode("han").ChildNodes;//获取bookstore节点的所有子节点 // foreach (XmlNode xn in nodeList)//遍历根节点的所有子节点 // { // XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型 // //if (xe.GetAttribute("lun") == "video1") // //{ // // strvideo1 = xe.InnerText.Trim(); //给视频赋值IP // //} // //if (xe.GetAttribute("lun") == "video2") // //{ // // strvideo2 = xe.InnerText.Trim(); // //} // //if (xe.GetAttribute("lun") == "video3") // //{ // // strvideo3 = xe.InnerText.Trim(); // //} // //if (xe.GetAttribute("lun") == "video4") // //{ // // strvideo4 = xe.InnerText.Trim(); // //} // if (xe.GetAttribute("lun") == "qu") // { // ConnectionManger.G_MineArea = xe.InnerText.Trim(); // } // //if (xe.GetAttribute("lun") == "Com") // //{ // // Com = xe.InnerText.Trim(); // //} // //if (xe.GetAttribute("lun") == "Com") // //{ // // Com = xe.InnerText.Trim(); // //} // } //} private void FrmNew_Load(object sender, EventArgs e) { //readxml(); ConnectionManger.readxml(); string str = "select * from user_table where user_area = '" + ConnectionManger.G_MineArea + "'"; DataTable dt = SQLHelper.GetDataSet(str, CommandType.Text).Tables[0]; for (int i = 0; i < dt.Rows.Count; i++) { CmbName.Items.Add(dt.Rows[i]["user_name"].ToString()); } dt.Dispose(); if (CmbName.Items.Count > 0) { CmbName.SelectedIndex = 0; } TxtMima.Focus(); TxtMima.Select(); }
/// <summary> /// 通过数据集来绑定数据 /// </summary> /// <param name="str">SQL语句/存储过程CommandType.Text/CommandType.StoredProcedure </param> /// <returns></returns> public static DataSet GetDataSet(string str, CommandType cmdtype) { SqlConnection conn = ConnectionManger.GetConnection(); DataSet ds = new DataSet(); try { SqlCommand cmd = new SqlCommand(str, conn); cmd.CommandType = cmdtype; SqlDataAdapter da = new SqlDataAdapter(str, conn); da.Fill(ds); return(ds); } catch { conn.Close(); throw; } }
/// <summary> /// 执行一条返回结果集的SqlCommand命令,通过专用的连接字符串。 /// 使用参数数组提供参数 /// </summary> /// <remarks> /// 使用示例: /// SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24)); /// </remarks> /// <param name="connectionString">一个有效的数据库连接字符串</param> /// <param name="commandType">SqlCommand命令类型 (存储过程, T-SQL语句, 等等。)</param> /// <param name="commandText">存储过程的名字或者 T-SQL 语句</param> /// <param name="commandParameters">以数组形式提供SqlCommand命令中用到的参数列表</param> /// <returns>返回一个包含结果的SqlDataReader</returns> public static SqlDataReader ExecuteReader(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); SqlConnection conn = ConnectionManger.GetConnection(); // 在这里使用try/catch处理是因为如果方法出现异常,则SqlDataReader就不存在, //CommandBehavior.CloseConnection的语句就不会执行,触发的异常由catch捕获。 //关闭数据库连接,并通过throw再次引发捕捉到的异常。 try { PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); cmd.Parameters.Clear(); return(rdr); } catch { conn.Close(); conn.Dispose(); throw; } }
private StringBuilder builder = new StringBuilder();//避免在事件处理方法中反复的创建,定义到外面。 private void ParameterSet_Load(object sender, EventArgs e) { ConnectionManger.readxml(); string[] ports = SerialPort.GetPortNames(); Array.Sort(ports); comboBox1.Items.AddRange(ports); model_com.Items.AddRange(ports); yb_Com.Items.AddRange(ports); comboBox1.Text = ConnectionManger.Card_Com; comboBox2.Text = ConnectionManger.Card_Baudrate; txt_video1.Text = ConnectionManger.strvideo1; txt_video2.Text = ConnectionManger.strvideo2; txt_video3.Text = ConnectionManger.strvideo3; txt_video4.Text = ConnectionManger.strvideo4; model_com.Text = ConnectionManger.Model_Com; model_txt.Text = ConnectionManger.Model_Com_Send_Txt; model_baute.SelectedIndex = 0; comboBox3.Text = "1"; yb_Com.Text = ConnectionManger.Yibiao_Com; yb_baute.Text = ConnectionManger.Yibiao_Baute; txt_ledip.Text = ConnectionManger.led_IP; txt_ledwight.Text = ConnectionManger.led_wight.ToString(); txt_ledheight.Text = ConnectionManger.led_height.ToString(); try { comm.BaudRate = int.Parse(ConnectionManger.Card_Baudrate); comm.PortName = ConnectionManger.Card_Com; if (!comm.IsOpen) { comm.Open(); } //添加事件注册 comm.DataReceived += comm_DataReceived; } catch (Exception ex) { } //根据当前串口对象,来判断操作 if (comm_model.IsOpen) { //打开时点击,则关闭串口 comm_model.Close(); } else { try { //关闭时点击,则设置好端口,波特率后打开 comm_model.PortName = ConnectionManger.Model_Com; comm_model.BaudRate = 9600; comm_model.Open(); } catch (Exception ex) { //捕获到异常信息,创建一个新的comm对象,之前的不能用了。 comm_model = new SerialPort(); } //添加事件注册 //comm_model.DataReceived += comm_DataReceived_Model; } }