The Perfect Day

分享技术,编写未来

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  69 随笔 :: 0 文章 :: 19 评论 :: 0 引用

2009年1月4日 #

private int iSendCount = 0;
        
private void Test(DSSendCollection dsRequest)
        {
            
//取基准数据做比较
            string strS1Name = Convert.ToString(dsSend.Sub1.Rows[0]["s1_name"]).Trim();
            
string strS1UUID = Convert.ToString(dsSend.Sub1.Rows[0]["uuid"]).Trim();
            

            DSSendCollection dsSendEdi 
= new DSSendCollection();
            
//对主表排序
            DSSendCollection.Sub1Row[] drs = dsRequest.Sub1.Select("""s1_Name,uuid"as DSSendCollection.Sub1Row[];
            
for (int i = 0; i < drs.Length; i++)
            {
                
if (drs[i].s1_Name.Trim() != strS1Name || drs[i].uuid.Trim() != strS1UUID)
                {
                    DataRow[] filterRows 
= dsRequest.Sub1.Select("s1_Name='" + strS1Name + "' and uuid='" + strS1UUID + "'");
                    
for (int j = 0; j < filterRows.Length; j++)
                    {
                        DataRow drShipment 
= filterRows[j];//主表的行
                        ImportRow(drShipment, dsSendEdi);
                    }
                    
//发送Edi
                    iSendCount++;
                    MessageBox.Show(
"发送EDI    "+""+iSendCount+"");
                    strS1Name 
= drs[i].s1_Name.Trim();
                    strS1UUID 
= drs[i].uuid.Trim();
                    dsSendEdi.Clear();
                    dsSendEdi.AcceptChanges();
                }
               
                
if (i == drs.Length - 1)
                {
                    
if (iSendCount == 0)
                    {
                        
//发送Edi:直接传dsRequest
                        
                    }
                    
else
                    {
                        DataRow[] filterRows 
= dsRequest.Sub1.Select("s1_Name='" + strS1Name + "' and uuid='" + strS1UUID + "'");
                        
for (int j = 0; j < filterRows.Length; j++)
                        {
                            DataRow drShipment 
= filterRows[j];
                            ImportRow(drShipment, dsSendEdi);
                        }
                        
//发送Edi
                        iSendCount++;
                        MessageBox.Show(
"发送EDI    " + "" + iSendCount + "");
                        dsSendEdi.Clear();
                        dsSendEdi.AcceptChanges();
                    }
                }
            }
posted @ 2009-01-04 16:37 StephenJu 阅读(23) | 评论 (0)编辑

2008年12月26日 #

 

public enum EnumName //tt:枚举名
        {
            [Description(
"t1描述")]//枚举标记t1的描述
            t1=1,//t1:枚举标记 1:枚举值
            [Description("t2描述")]
            t2
=2,
            [Description(
"t3描述")]
            t3
=3
        }

private string GetEnumDescription(Enum enumValue)
        {

            FieldInfo fieldInfo 
= enumValue.GetType().GetField(enumValue.ToString().Trim());
            DescriptionAttribute[] attritutes 
= fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), falseas DescriptionAttribute[];
            
return attritutes.Length > 0 ? attritutes[0].Description: enumValue.ToString();
        }

//根据枚举标记获得枚举值
            int iEnumValue = (int)Enum.Parse(typeof(EnumName), Convert.ToString(EnumName.t1));
//result:1
            
//根据枚举值得到枚举标记
            EnumName t_type = (EnumName)Enum.Parse(typeof(EnumName), Convert.ToString(iEnumValue), false);
//result:t1
            
//根据枚举标记获得其描述
            string strDescription = GetEnumDescription(t_type);
//result:t1描述
posted @ 2008-12-26 15:38 StephenJu 阅读(26) | 评论 (0)编辑

2008年12月11日 #

 

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 WindowsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            Init();
        }
        private string strConn = "server=.;uid=sa;pwd=sa;database=test";
        SqlConnection sqlconn = null;
        SqlDataAdapter sqlda1 = null, sqlda2 = null, sqlda3 = null, sqlda4 = null, sqlda5 = null,sqlda6=null;
        DataSet ds = new DataSet();
        DSSendCollection dsSend = new DSSendCollection();

        //------------------------------------------------------------------------------------------//
        private void Init()
        {
            this.btnSend.Click += new EventHandler(btnSend_Click);
            this.Load += new EventHandler(Form2_Load);
        }

        void Form2_Load(object sender, EventArgs e)
        {
            using (sqlconn = new SqlConnection(strConn))
            {
                sqlda1 = new SqlDataAdapter("select * from maint", sqlconn);
                sqlda1.Fill(ds, "MainT");
                sqlda2 = new SqlDataAdapter("select * from sub1", sqlconn);
                sqlda2.Fill(ds, "Sub1");
                sqlda3 = new SqlDataAdapter("select * from sub2", sqlconn);
                sqlda3.Fill(ds, "Sub2");
                sqlda4 = new SqlDataAdapter("select * from sub3", sqlconn);
                sqlda4.Fill(ds, "Sub3");
                sqlda5 = new SqlDataAdapter("select * from sub4", sqlconn);
                sqlda5.Fill(ds, "Sub4");
                sqlda6 = new SqlDataAdapter("select * from sub5", sqlconn);
                sqlda6.Fill(ds, "Sub5");
                this.dsSend.Merge(ds);
                this.ds.AcceptChanges();
                //dsSend.WriteXml("d:\\dsSendTest.xml");
                //MessageBox.Show("WriteXml成功!");
            }
        }

        void btnSend_Click(object sender, EventArgs e)
        {
            SendDS(50);
        }


        private void ImportRow(DataRow drSource, DataSet dsSplit)
        {
            string tbName = drSource.Table.TableName.ToString().Trim();
            if (!dsSend.Tables.Contains(tbName)) return;
            dsSplit.Tables[tbName].ImportRow(drSource);
            DataRelationCollection relations = drSource.Table.ChildRelations;
            foreach (DataRelation relation in relations)
            {
                DataRow[] drChilds = drSource.GetChildRows(relation);
                if (drChilds != null || drChilds.Length > 0)
                {
                    foreach (DataRow drChild in drChilds)
                    {
                        ImportRow(drChild, dsSplit);
                    }
                }
            }
        }

     
        int iCurrent = 0;
        int iPageCurrent = 1;
        private void SendDS(int iPageSize)
        {
            int iStartPos = 0;
            int iEndPos = iPageSize;

            int iMaxCount = this.dsSend.MainT.Rows.Count;
            int iLastPageSize = iMaxCount % iPageSize;
            int iPageCount = ((iLastPageSize < iPageSize) && iLastPageSize != 0) ? iMaxCount / iPageSize + 1 : iMaxCount / iPageSize;

            //DSSendCollection dsSplit = this.dsSend.Clone() as DSSendCollection;
            if (dsSend.MainT.Rows.Count < iPageSize)
                SendMessage(iPageCount);
            else
            {
                //从元数据源复制记录行
                for (int iStep = 0; iStep < iPageCount; iStep++)
                {
                    DSSendCollection dsSplit=new DSSendCollection();
                    for (int i = iStartPos; i < iEndPos; i++)
                    {
                  
                        /*
                        if (this.dsSend.MainT.Rows[i] != null)
                            dsSplit.MainT.ImportRow(this.dsSend.MainT.Rows[i]);//导入主表数据

                        //导入从表数据--Method1
                        int iChildTBCount = dsSend.MainT.ChildRelations.Count;
                        for (int j = 0; j < iChildTBCount; j++)
                        {
                            foreach (DataRow row in dsSend.MainT.ChildRelations[j].ChildTable.Select("uuid='" + dsSend.MainT.Rows[i]["UUID"].ToString().Trim() + "'"))
                            {
                                if (row != null)
                                    dsSplit.MainT.ChildRelations[j].ChildTable.ImportRow(row);
                            }
                        }

                        //导入从表数据--Method2
                        //DataRelationCollection relation = this.dsSend.MainT.ChildRelations;
                        //
                        //int iTBCount = this.dsSend.Tables.Count;
                        //for (int j = 0; j < iTBCount; j++)
                        //{
                        //    if (dsSend.Tables[j].TableName != "MainT")
                        //    {
                        //        foreach (DataRow row in this.dsSend.Tables[j].Select("uuid='" + this.dsSend.MainT.Rows[i]["uuid"].ToString().Trim() + "'"))
                        //        {
                        //            if (row != null)
                        //                dsSplit.Tables[j].ImportRow(row);
                        //        }
                        //    }
                        //}
                        //
                        iCurrent++;
                        */


                        ImportRow(dsSend.MainT.Rows[i], dsSplit);
                        iCurrent++;
                    }
                    iPageCurrent++;
                    iStartPos = iCurrent;
                    if (iPageCurrent == iPageCount)
                        iEndPos = iMaxCount;
                    else
                        iEndPos = iPageSize * iPageCurrent;

                    dsSplit.WriteXml("d:\\dsSplit.xml");
                    SendMessage(iPageCount);
                    dsSplit.Clear();
                }
                iPageCurrent = 1;
            }
        }

        int iCall = 0;
        private void SendMessage(int count)
        {
            iCall++;
            if (iCall == count)
            {
                MessageBox.Show("成功发送:" + "" + count + "次");
                iCall = 0;
                this.Close();
            }
        }

    }
}

posted @ 2008-12-11 00:26 StephenJu 阅读(45) | 评论 (0)编辑

2008年6月17日 #

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace Sys
{
    [Serializable] 
//指示一个类可以序列化
    public class Element
    {
        
//复制对象
        public object DeepClone()
        {
            
object result = null;
            
using (MemoryStream ms = new MemoryStream()) //内存流
            {
                BinaryFormatter bf 
= new BinaryFormatter();//以二进制格式序列化对象
                bf.Serialize(ms, this);//将对象序列化为内存流
                ms.Seek(0, SeekOrigin.Begin);
                result 
= bf.Deserialize(ms); //将内存流反序列化为对象
            }
            
return result;
        }

        
//保存对象到文件
        
//(对象序列化到ms)→(ms写入byte[])→(byte[]写入流)
        public void WriteInstanceToFile(string fileName)
        {
            
using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter bf 
= new BinaryFormatter();
                bf.Serialize(ms, 
this);
                ms.Seek(
0, SeekOrigin.Begin);
                
byte[] buffer = new byte[ms.Length];
                ms.Read(buffer, 
0, buffer.Length);
                ms.Seek(
0, SeekOrigin.Begin);

                FileStream stream 
= new FileStream(fileName, FileMode.Create);
                stream.Write(buffer,
0,buffer.Length);
            }
        }

        
//从文件中读取对象
        public static object ReadInstanceFromFile(string fileName)
        {
            
object result = null;
            FileStream stream 
= new FileStream(fileName, FileMode.Open);//以二进制格式序列化对象
            BinaryFormatter bf = new BinaryFormatter();
            result 
= bf.Deserialize((Stream)stream); //流反序列化为对象
            stream.Close();
            
return result;
        }
    }
}
posted @ 2008-06-17 13:04 StephenJu 阅读(97) | 评论 (0)编辑

2008年6月10日 #



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 Sys
{
    
public partial class Form3 : Form
    {
        
public Form3()
        {
            InitializeComponent();
        }

        
private void Form3_Load(object sender, EventArgs e)
        {
            dataGridView1.AllowUserToAddRows 
= false;
            dataGridView1.AutoGenerateColumns 
= false;

            
using (SqlConnection sqlconn = new SqlConnection("server=.;uid=sa;pwd=sa;database=xx"))
            {
                
string sql1 = @"select userid,fullname,a.[description],b.ugroupid
                                from users a left outer join usergroup b 
                                on a.grade=b.ugroupid
";
                
string sql2 = "select distinct ugroupid,[description] from usergroup";

                DataGridViewComboBoxColumn dgvComboBoxColumn 
= dataGridView1.Columns["grade"as DataGridViewComboBoxColumn;
                dgvComboBoxColumn.DataPropertyName 
= "ugroupid";
                dgvComboBoxColumn.DataSource 
= GetTable(sql2).DefaultView;//必须在设置dataGridView1的DataSource的属性前设置
                dgvComboBoxColumn.DisplayMember = "description";
                dgvComboBoxColumn.ValueMember 
= "ugroupid";

                dataGridView1.DataSource 
= GetTable(sql1).DefaultView;//一定要在dgvComboBoxColumn的DataSource后设置
            }
        }

        
private DataTable GetTable(string sql)
        {
            
using (SqlConnection sqlconn = new SqlConnection("server=.;uid=sa;pwd=yuling1310;database=smls"))
            {
                DataTable dt 
= new DataTable();
                SqlDataAdapter sqlda 
= new SqlDataAdapter(sql, sqlconn);
                sqlda.Fill(dt);
                
return dt;
            }
        }
    }
}

注意事项:
.注意其ValueMember的DataType与DataPropertyName对应的列的DataType要相同,他不会为你做类型转换的。
2.编程设置显示样式时注意一定要在设置DataSource之前设置DataGridViewComboBoxColumn的DataSource等属性。
posted @ 2008-06-10 11:45 StephenJu 阅读(450) | 评论 (1)编辑

2008年5月28日 #

Validate Data in the Windows Forms DataGridView Control  
Samples:
private void dataGridView1_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
// Validate the CompanyName entry by disallowing empty strings.
    if (dataGridView1.Columns[e.ColumnIndex].Name == "CompanyName")
{
if (e.FormattedValue.ToString() == String.Empty)
{
dataGridView1.Rows[e.RowIndex].ErrorText =
"Company Name must not be empty";
e.Cancel = true;
}
}
}
posted @ 2008-05-28 21:29 StephenJu 阅读(351) | 评论 (0)编辑

2008年5月26日 #

//如果有超过一屏的数据,想通过关键字查找到相关记录,然后再定位之,做法如下:
//遍历所有行,把某单元格的值和关键字对比,找到后清除所有选择行,然后把当前行设为选择,然后把grid的CurrentCell设置为当前行的某个可见单元格即可,效果就会自动跳到定位好的行上。
     string str = textBox1.Text.Trim();
            foreach (DataGridViewRow dgvRow in dgvRoleInfo.Rows)
            {
                if(dgvRow.Cells[2].Value.ToString().StartsWith(str))
                {
                    dgvRoleInfo.ClearSelection();
                    dgvRow.Selected = true;
                    dgvRoleInfo.CurrentCell = dgvRow.Cells[2];
                    break;
                }
            }
posted @ 2008-05-26 15:33 StephenJu 阅读(98) | 评论 (1)编辑

Boolean createdNew; //返回是否赋予了使用线程的互斥体初始所属权
System.Threading.Mutex instance = new System.Threading.Mutex(true"MutexName"out createdNew); //同步基元变量
if (createdNew) //赋予了线程初始所属权,也就是首次使用互斥体
{
    Application.Run(
new Form1()); /s/这句是系统自动写的
    instance.ReleaseMutex();
}
else
{
    MessageBox.Show(
"已经启动了一个程序,请先退出!","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
    Application.Exit();
}
posted @ 2008-05-26 15:31 StephenJu 阅读(116) | 评论 (0)编辑

BULK INSERT 数据库名.dbo.[表名] --表名
   FROM 'd:\create_report.txt'--文本文件名
   WITH
      (
         FIELDTERMINATOR = '|',--字段结束符
         ROWTERMINATOR = '|\n'--行结束符
      )
posted @ 2008-05-26 15:30 StephenJu 阅读(36) | 评论 (0)编辑

        public class PositionData
        {
            
private string name; //字段
            private string ticker;//字段
            private string shares;//字段

            
public PositionData()
            {

            }

            
public PositionData(string name, string ticker, string shares)
            {
                
this.name = name;
                
this.ticker = ticker;
                
this.shares = shares;
            }

            
public string Name //属性
            {
                
get
                {
                    
return name;
                }
            }

            
public string Ticker //属性
            {
                
get
                {
                    
return ticker;
                }
            }

            
public string Shares //属性
            {
                
get
                {
                    
return shares;
                }
            }
        }

        ArrayList values 
= new ArrayList();
        values.Add(
new PositionData("Microsoft""Msft""150 共享")); 
        values.Add(
new PositionData("Intel""Intc""25 共享")); 
        values.Add(
new PositionData("Dell""Dell""115 共享"));
        
foreach (PositionData da in values)
        {
           
if (da.Name == "Microsoft")
              MessageBox.Show(da.Name.ToString()
+" "+da.Ticker.ToString()+" "+da.Shares.ToString(),"Info");
        }



//******************************使用泛******************************//
        
    List
<PositionData> tmp = new List<PositionData>();
        tmp.Add(
new PositionData("Microsoft""Msft""150 共享"));
        tmp.Add(
new PositionData("Intel""Intc""25 共享"));
        tmp.Add(
new PositionData("Dell""Dell""115 共享"));
        
for (int i = 0; i < tmp.Count; i++)
            MessageBox.Show(tmp[i].Name 
+ " " + tmp[i].Ticker + " " + tmp[i].Shares, "Info");
posted @ 2008-05-26 15:28 StephenJu 阅读(422) | 评论 (0)编辑