数据表中有一个字段是联系方式字段,里面包括Eamil、Phone、Fax三部分,我现在想把它拆分为三个字段,并把对应信息填入其中。我是在我inForm中制作的,我的想法是找到这个联系方式字段时,双击它,让它在原表中自动生成三个字段,并填入值,请问这个想法,能实现吗?有什么具体解决办法吗?

解决方案 »

  1.   

    不会增加字段 什么意思?
    --增加字段
    alter table TB
    add  Eamil varchar(20)
      

  2.   

    alter table TB
    add  Eamil varchar(20),Phone varchar(20),Fax varchar(20)
    --删除字段
    alter table TB
    drop column Eamil,Phone,Fax
      

  3.   

    我的意思是,将原来联系方式的一个字段的具体信息,加入到新建的三个字段的信息中去如联系方式叫Info,其中的内容为Email:aa·126.com Phone:025-123456 Fax:021-789456把新信息分别放入Email  aa·126.com Phone  025-123456 Fax    021-789456
      

  4.   

      try
                {
                    using (SqlConnection con = new SqlConnection("Server=" + strServerName + ";database=" + strDataName + ";Uid=" + strUser + ";Pwd=" + strPwd))
                    {
                        sql = "select "+ str + " from "+strDataTable; //str代表联系方式字段
                        con.Open();
                        SqlCommand cmd = new SqlCommand(sql, con);
                        SqlDataReader reader = cmd.ExecuteReader();
                        //取出联系方式一个集合以后,怎么拆分啊?
                          //拆分完,如何存放到三个新字段中呢?
            }
                }
      

  5.   

    create table tb(col varchar(100))
    insert into tb values('Email:aa·126.com Phone:025-123456 Fax:021-789456')
    goselect 
    replace(parsename(replace(replace(col , '.' , '#'),' ','.'),3),'#','.'),
    replace(parsename(replace(replace(col , '.' , '#'),' ','.'),2),'#','.'),
    replace(parsename(replace(replace(col , '.' , '#'),' ','.'),1),'#','.')
    from tbdrop table tb/*
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
    Email:aa·126.com                                                                                                                                                                                                                                                 Phone:025-123456                                                                                                                                                                                                                                                 Fax:021-789456(所影响的行数为 1 行)*/create table tb(col varchar(100))
    insert into tb values('Email:aa·126.com Phone:025-123456 Fax:021-789456')
    goselect replace(parsename(replace(replace(col , '.' , '#'),' ','.'),3),'#','.') from tb
    union all
    select replace(parsename(replace(replace(col , '.' , '#'),' ','.'),2),'#','.') from tb
    union all
    select replace(parsename(replace(replace(col , '.' , '#'),' ','.'),1),'#','.') from tb
    drop table tb/*
                          
    ----------------------
    Email:aa·126.com
    Phone:025-123456
    Fax:021-789456(所影响的行数为 3 行)*/
      

  6.   

    楼上的,好厉害啊,我有点看不太懂,sql初学,还想问一下,我说的联系方式是一个字段,而且这个字段有很多行值,我是不是不能用insert into tb values('Email:aa·126.com Phone:025-123456 Fax:021-789456')??? 
      

  7.   

    你只需要这两段,不知道哪段适合你?
    select 
    replace(parsename(replace(replace(col , '.' , '#'),' ','.'),3),'#','.'),
    replace(parsename(replace(replace(col , '.' , '#'),' ','.'),2),'#','.'),
    replace(parsename(replace(replace(col , '.' , '#'),' ','.'),1),'#','.')
    from tbselect replace(parsename(replace(replace(col , '.' , '#'),' ','.'),3),'#','.') from tb
    union all
    select replace(parsename(replace(replace(col , '.' , '#'),' ','.'),2),'#','.') from tb
    union all
    select replace(parsename(replace(replace(col , '.' , '#'),' ','.'),1),'#','.') from tb至于这句
    insert into tb values('Email:aa·126.com Phone:025-123456 Fax:021-789456')
    是我的测试语句.
      

  8.   

    好像楼上的方法,只能进行拆分?select replace(parsename(replace(replace(col , '.' , '#'),' ','.'),3),'#','.') from tb
    union all
    select replace(parsename(replace(replace(col , '.' , '#'),' ','.'),2),'#','.') from tb
    union all
    select replace(parsename(replace(replace(col , '.' , '#'),' ','.'),1),'#','.') from tb如何对拆分完的数据,加入到对应字段中呢?还有,如果我想把每行的Email: Phone: Fax: 都省去,还要把 · 变为@ ,这个语法???? 
      

  9.   

    我是这么写的,挺繁琐,挺笨的方法,还有一个问题,就是SqlCommand可以嵌套吗?提示这个地方有问题?  try
                {
                    using (SqlConnection con = new SqlConnection("Server=" + strServerName + ";database=" + strDataName + ";Uid=" + strUser + ";Pwd=" + strPwd))
                    {
                        string sql = "select " + str + " from " + strDataTable;
                        sql += " alter table "  + strDataTable + " add Email varchar(50),Phone varchar(50),Fax varchar(50)"; //生成三个新字段                    con.Open();
                        SqlCommand cmd = new SqlCommand(sql, con);
                        SqlDataReader reader = cmd.ExecuteReader();
                        int temp = 0;   //计数变量 
                        while (reader.Read())
                        {
                            string strInfo = reader[temp].ToString().ToLower().Trim();//小写
                            int strLength = strInfo.Length;         //长度                        int emailNum = strInfo.IndexOf("email:");
                            int phoneNum = strInfo.IndexOf("phone:");
                            int faxNum = strInfo.IndexOf("fax:");                        string email = strInfo.Substring(emailNum, phoneNum - emailNum).Replace("email:", "").Replace("·", "@");
                            string phone = strInfo.Substring(phoneNum, faxNum - phoneNum).Replace("phone:", "");
                            string fax = strInfo.Substring(faxNum, strLength - faxNum).Replace("fax:", "");
                            
                            temp++;                        string insertsql = "insert into "+strDataTable+ "(Eamil,Phone,Fax) values("+ email+","+phone+","+fax +")";
                            
                            cmd.ExecuteNonQuery();
                            
                        }
                        reader.Close();                    SqlDataReader sdr = cmd.ExecuteReader();
                        dataGridView1.DataSource = null;
                        dataGridView1.DataSource = sdr;
                        sdr.Close();
                        
                        con.Close();                }
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message); 
                }
      

  10.   

    直接插入吗?
    insert into tb2 
    select  
    replace(parsename(replace(replace(col , '.' , '#'),' ','.'),3),'#','.'),
    replace(parsename(replace(replace(col , '.' , '#'),' ','.'),2),'#','.'),
    replace(parsename(replace(replace(col , '.' , '#'),' ','.'),1),'#','.')
    from tb1假设两表对应更新吗?
    update tb2
    set 
    c1 = replace(parsename(replace(replace(tb1.col , '.' , '#'),' ','.'),3),'#','.'),
    c2 = replace(parsename(replace(replace(tb1.col , '.' , '#'),' ','.'),3),'#','.'),
    c3 = replace(parsename(replace(replace(tb1.col , '.' , '#'),' ','.'),3),'#','.')
    from tb2 , tb1
    where tb2.关键字 = tb1.关键字
      

  11.   

    就是这里有问题
                            string insertsql = "insert into "+strDataTable+ "(Eamil,Phone,Fax) values("+ email+","+phone+","+fax +")";
                            SqlCommand cmd1 = new SqlCommand(insertsql, con);  //这里报错,说已打开与当前冲突
                            cmd.ExecuteNonQuery();
    刚才少贴了一句怎么改啊?请教!