第20课-数据库开发及ado.net 可空值类型,资料管理器,多条件查询,Case

核心提示:using System;using System.Collections.Generic;using
System.ComponentModel;using System.Data;using System.Drawing;using
System.Text;using System.Windows.Forms;//所引入的命名空间

第20课-数据库开发及ado.net

怎么将输入内容修改为保存到本地 50C
现在我这代码是输入内容用Codesoft打印出来,现在想修改为将输入内容保存为.txt格式到本地或网络盘,如txtTargetQty为4则每输入4个保存为一个文件,并且名称以第一个输入内容命名
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using System.Diagnostics;
using System.Web;
using System.Net;
using System.Collections;

using System; using System.Collections.Generic; using
System.ComponentModel; using System.Data; using System.Drawing; using
System.Text; using System.Windows.Forms;

可空值类型,资料管理器,多条件查询,Case

namespace PrintSN
{
public partial class MainForm : Form
{
private DataTable myDs = new DataTable();

//所引入的命名空间 using System.Data.Sql;

SqlHelper

 public MainForm() { InitializeComponent(); } private void txtInput_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == Keys.Enter) { string strInput = txtInput.Text; txtTargetQty.Text = Common.getConfigValueKey("PrintSNCount"); if (strInput.Equals { undo(); txtInput.SelectAll(); txtInput.Focus(); return; } else { if (dataGridView1.Rows.Count >0) { string strSN = ""; for (int i = 0; i < dataGridView1.Rows.Count; i++) { strSN = dataGridView1.Rows[i].Cells[0].Value.ToString(); if (strInput.Equals { ShowInformation("SN 重复,请扫描其他的SN ! ", "NG"); txtInput.SelectAll(); txtInput.Focus(); return; } } } if (txtScanQty.Text.Equals((Int32.Parse(txtTargetQty.Text) - 1).ToString { int index = this.dataGridView1.Rows.Add(); this.dataGridView1.Rows[index].Cells[0].Value = strInput; ArrayList SnDetail = new ArrayList(); for (int i = 0; i < dataGridView1.Rows.Count; i++) { SnDetail.Add(dataGridView1.Rows[i].Cells[0].Value.ToString; } SoftCodePrint Print = new SoftCodePrint(); Print.PrintSNLabel(strInput, SnDetail); txtScanQty.Text = "0"; dataGridView1.Rows.Clear(); ShowInformation("打印完成,请扫描的下一個SN ! ", "OK"); } else { txtScanQty.Text = (Int32.Parse(txtScanQty.Text) + 1).ToString(); ShowInformation("请扫描的下一個SN ! ", "OK"); int index = this.dataGridView1.Rows.Add(); this.dataGridView1.Rows[index].Cells[0].Value = strInput; } txtInput.SelectAll(); txtInput.Focus(); return; } } } private void undo() { //txtTargetQty.Text = ""; txtScanQty.Text = "0"; dataGridView1.Rows.Clear(); ShowInformation("UNDO 操作成功,請掃描 SN! ", "OK"); } private void ShowInformation(string sInfo, string sType) { lblShowMessageInfo.Text = sInfo; if (sType.Equals { lblShowMessageInfo.BackColor = System.Drawing.Color.FromArgb(((System.Byte), ((System.Byte), ((System.Byte); } if (sType.Equals { lblShowMessageInfo.BackColor = System.Drawing.Color.FromArgb(((System.Byte), ((System.Byte), ((System.Byte); } if (sType.Equals { lblShowMessageInfo.BackColor = System.Drawing.Color.Red; } } private void MainForm_Load(object sender, EventArgs e) { txtTargetQty.Text = Common.getConfigValueKey("PrintSNCount"); } private void MainForm_Shown(object sender, EventArgs e) { txtInput.SelectAll(); txtInput.Focus(); }}

namespace EnumSqlServer { public partial class MainForm : Form { public
MainForm() { InitializeComponent(); }

using System;

}

private void MainForm_Load(object sender, EventArgs e) {
//枚举本地网络内所有可用SqlServer实例 SqlDataSourceEnumerator instance =
SqlDataSourceEnumerator.Instance; //返回
//—————————————————————————-
// ServerName|服务器的名称
//—————————————————————————-
// InstanceName|服务器实例的名称。如果服务器作为默认实例运行,则为空白
//—————————————————————————-
// IsClustered|指示服务器是否属于群集
//—————————————————————————-
// Version|服务器的版本(SQLServer2000为8.00.x,SQLServer2005为9.00.x)
//—————————————————————————-
DataTable dt = instance.GetDataSources(); //绑定
cboServerList.DataSource = CreateCboDataSource(dt); }

using System.Collections.Generic;

/// summary /// 取得ComBox的数据源 /// /summary /// param name=dt/param
/// returns/returns private Liststring CreateCboDataSource(DataTable dt)
{ Liststring list = new Liststring();

using System.Configuration;

foreach (DataRow dr in dt.Rows) { //只取ServerName
list.Add(dr[0].ToString()); }

using System.Data;

return list; } } }

using System.Data.SqlClient;

using System.Text;

namespace _02省市联动

{

   public static  class SqlHelper

    {

       //常量 无法修改

       private const string A = “aaa”;

       //readonly 是一个只读的变量,只能在声明或构造函数中修改

      
//只读变量只能在构造函数或者是一开始声明变量的时候赋值,不允许在其它地方为变量赋值。

       //连接字符串

       private static readonly string conStr =
ConfigurationManager.ConnectionStrings[“constr”].ConnectionString;

    

     

       /// <summary>

       /// 封装一个ExecuteNonQuery方法,执行insert、delete、updata

       /// </summary>

       /// <param name=”sql”></param>

       /// <param name=”pms”></param>

       /// <returns></returns>

       public static int ExecuteQuery(string sql,params SqlParameter[]
pms)

       {

           using (SqlConnection con=new SqlConnection (conStr))

           {

               using (SqlCommand com=new SqlCommand (sql,con))

               {

                   if (pms!=null)

                   {

                       com.Parameters.AddRange(pms);

                   }

                   con.Open();

                   return com.ExecuteNonQuery();

               }

           }

       }

       /// <summary>

       /// 封装返回一个单个值的方法

       /// </summary>

       /// <returns></returns>

       public static object ExecuteScalar(string sql,params
SqlParameter[] pms)

       {

           using (SqlConnection con = new SqlConnection(conStr))

           {

               using (SqlCommand com=new SqlCommand (sql,con))

               {

                   if (pms!=null)

                   {

                       com.Parameters.AddRange(pms);

                   }

                   con.Open();

                   return com.ExecuteScalar();

               }

           }

       }

       /// <summary>

       /// 封装一个返回SqlDataReader的方法

       /// </summary>

       /// <returns></returns>

       public static SqlDataReader  ExecuteReader(string sql,params
SqlParameter[] pms)

       {

              SqlConnection con=new SqlConnection (conStr);

               using (SqlCommand com=new SqlCommand (sql,con))

               {

                   if (pms!=null)

                   {

                       com.Parameters.AddRange(pms);

                   }

                   try

                   {

                       con.Open();

                       return
com.ExecuteReader(System.Data.CommandBehavior.CloseConnection);   

                   }

                   catch (Exception)

                   {

                       con.Close();

                       con.Dispose();

                       throw;

                   }

                   

                }

       }

       public static DataTable ExecuteDataTable(string sql,params
SqlParameter[] pms)

       {

           DataTable dt = new DataTable();

           using (SqlDataAdapter adapter=new SqlDataAdapter (sql,conStr
))

           {

               //如何添加参数

               adapter.Fill(dt);

           }

           return dt;

       }

    }

}

省市联动

using System;

using System.Collections.Generic;

using System.Text;

 

namespace _02省市联动

{

    public  class ProvinceItem

    {

        public int AreaId {get ;set;}

        public string AreaName{get ;set;}

        public int AreaPid{get ;set;}

        public override string ToString()

        {

            return this.AreaName;

        }

    }

}

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

 

namespace _02省市联动

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

           //加载省份信息到第一个ComboBox

            LoadProvince();

            //设置俩个下拉菜单的默认值为“请选择”

            comboBox1.SelectedIndex = 0;

            comboBox2.SelectedIndex = 0;

       

        }

        private void LoadProvince()

        {

            string sql = “select * from TblArea where AreaPId  =0”;

            using (SqlDataReader reader=SqlHelper .ExecuteReader(sql))

            {

                {

                if (reader .HasRows)

                    while(reader .Read())

                    {

                        ProvinceItem item = new ProvinceItem();

                        item.AreaId = reader.GetInt32(0);

                        item.AreaName = reader.GetString(1);

                        item.AreaPid = reader.GetInt32(2);

                        comboBox1.Items.Add(item);

                    }

                }

            }

            //为ComboBox 增加一个“请选择”

            ProvinceItem itemDefault = new ProvinceItem();

            itemDefault.AreaId = -1;

            itemDefault.AreaName = “请选择”;

            comboBox1.Items.Insert(0, itemDefault);

        }

        /// <summary>

        /// 下拉菜单的选择项改变事件

        /// </summary>

        /// <param name=”sender”></param>

        /// <param name=”e”></param>

        private void comboBox1_SelectedValueChanged(object sender,
EventArgs e)

        {

            //获取当前用户选择的项

            if (comboBox1.SelectedIndex > 0)

            {

               
//加载第二个下来菜单,数据来源:根据第一个下拉菜单用户选项的AeraId来查询该项的所有子项

                //获取当前选中项的ID

                ProvinceItem item=comboBox1.SelectedItem as
ProvinceItem;

                int areaId = item.AreaId;

                LoadCity(areaId);

            }

        }

        private void LoadCity(int areaId)

        {

            comboBox2.Items.Clear();

            string sql = “select * from TblArea where AreaPId=@aid”;

            using (SqlDataReader reader = SqlHelper.ExecuteReader(sql,
new SqlParameter(“@aid”, areaId)))

            {

                if (reader .HasRows )

                {

                    while (reader.Read())

                    {

                         ProvinceItem item = new ProvinceItem();

                        item.AreaId = reader.GetInt32(0);

                        item.AreaName = reader.GetString(1);

                        item.AreaPid = reader.GetInt32(2);

                        comboBox2.Items.Add(item);

                    }

                }

            }

            //也添加一个“请选择”

            ProvinceItem itemDefault = new ProvinceItem();

            itemDefault.AreaId = -1;

            itemDefault.AreaName = “请选择”;

            comboBox2.Items.Insert(0, itemDefault);

            comboBox2.SelectedIndex = 0;

        }

    }

}

省市数据递归加载到TreeView

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

 

namespace _03省市数据递归加载到TreeView

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            ////1.把所有的省份及直辖市加载到TreeView的根节点上

            //List<Area> listProvince = GetSubItemByParentId(0);

            //foreach (Area  item in listProvince)

            //{

            //    treeView1.Nodes.Add(item .AreaName );

            //}

            //递归将省市加载到TreeView中

            LoadDataToTree(treeView1.Nodes,0);

 

 

        }

 

        private void LoadDataToTree(TreeNodeCollection
treeNodeCollection, int pid)

        {

            //1.根据指定的Pid获取该城市下的子城市

            List<Area> listCity = GetSubItemByParentId(pid);

            //2.遍历将获取到的数据绑定到TreeNodeCollection节点集合中

            foreach (var item in listCity )

            {

                //返回刚刚添加的这个节点

                TreeNode node = treeNodeCollection.Add(item.AreaName);

                LoadDataToTree(node.Nodes ,item.Areaid);

            }

        }

 

        //封装一个方法,根据父ID查询下面的所有子项

        private List<Area> GetSubItemByParentId(int pid)

        {

            List<Area> list = new List<Area>();

            string sql = “select * from TblArea where areaPid=@pid”;

            using (SqlDataReader reader = SqlHelper .ExecuteReader
(sql,new SqlParameter (“@pid”,pid)))

            {

                if (reader .HasRows )

                {

                    while (reader .Read ())

                    {

                        Area model = new Area();

                        model.Areaid = reader.GetInt32(0);

                        model.AreaName = reader.GetString(1);

                        model.AreaPid = reader.GetInt32(2);

                        list.Add(model);

                    }

                   

                }

            }

            return list;

        }

    }

}

using System;

using System.Collections.Generic;

using System.Text;

 

namespace _03省市数据递归加载到TreeView

{

   public  class Area

    {

       public int Areaid { get; set; }

       public string AreaName { get; set; }

       public int AreaPid { get; set; }

    }

}

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注