cn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["cn"]);
        cn.Open();
        SqlCommand cmd = new SqlCommand("select (*)from book where booktime='" + TextBox1.Text  + "'", cn);
            if (booktime.Equals(DBNull.Value))
        {
            cn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["cn"]);
            cn.Open();
            SqlCommand cmd1 = new SqlCommand("insert into book(booktime)values('" + TextBox1.Text.Replace("'", "''") + "',)", cn);
            cmd.ExecuteNonQuery();
        }
问题是取出来的值需要进行声明才能在if语句中使用,但是如果取到的是空值该如何声明呢,目的是如果取到的是空值就把TextBox1的值写入这一列

解决方案 »

  1.   

    cn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["cn"]);
            cn.Open();
            SqlCommand cmd = new SqlCommand("select top 1 booktime from book where booktime='" + TextBox1.Text + "'", cn);
            if (booktime.Equals(cmd.ExecuteScalar()))
            {
                cn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["cn"]);
                cn.Open();
                SqlCommand cmd1 = new SqlCommand("insert into book(booktime)values('" + TextBox1.Text.Replace("'", "''") + "',)", cn);
                cmd.ExecuteNonQuery();
            }
      

  2.   

      if (booktime.Equals(cmd.ExecuteScalar()))这里还是说当前上下文不存在名称"booktime"
      

  3.   

    你可以在代码中写存储过程。
    例如:string sql=@"
                       declare @booktime string
                       select @booktime=booktime from book where 条件
                       if(@booktime=='')
                         begin
                         insert into book(booktime) values('{0}')
                         end";
    然后,sql=string.format(sql," +TextBox1.Text.tostring()+ ");
    最后,执行入库操作。试试看,惊喜等着你!
      

  4.   

    insert into book(booktime) values('{0}')这句话写错了。应该是update book set booktime='{0}' where 条件!
      

  5.   

    报错说string不包含format的定义,该怎么修改呢
      

  6.   

    string.Format //Format,F要大写
      

  7.   

    这次值还是没有写入booktime这一列中,还有个问题是,如果我要同时把几个不同的值写入不同的几列中,如:TextBox1写入booktime列,8:00写入T1列,8:30写入T2列,这个代码应该怎么改,谢谢大神了
      

  8.   

    根据你的需求,应该是对已有的进行更新,那就应该用update book set booktime='{0}',T1='{1}',T2='{2}' where 条件
    sql=string.Format(sql,替换{0}的字符串,替换{1}的字符串,替换{2}的字符串);
      

  9.   

    这个存储过程的目的就是取出符合条件的记录中的booktime,判断它是不是为空,如果为空,就把这些符合条件的并且booktime为空的的记录全部update一下,我觉得你的需求不是insert一下!
      

  10.   

    那就insert,这应该会吧!insert into book(booktime,T1,T2) values('{0}','{1}','{2}')
      

  11.   

    那这样的话在前面string sql=@"
                       declare @booktime string
                       select @booktime=booktime from book where 条件
    这里是不是还要加上declare T1
                      declare T2
                       select T1
                       select T2
    同时获得多个列应该怎么写?
      

  12.   

    我想了一下的确用update比较好,刚才的问题是在执行存储的过程之后TextBox1中的值没有写入booktime这一列里,我看了一下数据库里还是null
      

  13.   

      protected void Button4_Click(object sender, EventArgs e)
            {
                string sql = @"
                       declare @booktime string
                        declare @ T1 string
                        declare T2 string
                       select @T1=T1 from book
                       select @T2=T2 from book
                       select @booktime=booktime from book 
                       if(@booktime=='')
                         begin
                         update book set booktime='{0}',T1='{1}',T2='{2}'
                          end";
          sql=string.Format(sql," +TextBox1.Text.ToString()+ ","8:30","9:00");
            }
    select...where后面没什么条件所以就没写,但是值还是没有写进数据库里,这个代码还有哪些问题
      

  14.   

    你打断点,把  sql=string.Format(sql," +TextBox1.Text.ToString()+ ","8:30","9:00");执行后的sql拿出来,在数据库中执行一下,看有没有错误
      

  15.   

    我试了一下不知道对不对,sql中说是 if(@booktime=='')这里有错误
      

  16.   

    查询的时候由于没有条件,查出来的booktime可能有很多条,由于@booktime只能被赋一个值。我觉得错误原因应该在这!
      

  17.   

    能不能把要赋值给booktime的这一行加个条件确定为booktime是空值的最靠前的一行,这样应该可以解决问题,也就是说只把值写入最靠前的空行中
      

  18.   

    你试试select @booktime=top 1 booktime from book where booktime=''看能不能取到所有booktime为空的第一条!
      

  19.   

    @加上了的,但是还是没有值被写入,假设select @booktime=top 1 booktime from book where booktime=''已经取到了booktime是空值的最靠前,那后面的 if(@booktime=='')是否就失去作用了呢,我感觉还是没有取到最靠前的一行
      

  20.   

    LZ  我来说说我一般怎么做的   
                
                SqlConnection con = new SqlConnection();
                //本人比较喜欢SqlDataAdapter 来操作数据库
                SqlDataAdapter sda = new SqlDataAdapter("",con);
                DataTable dt = new System.Data.DataTable();
                sda.Fill(dt);
                for (int a = 0; a < dt.Rows.Count; a++)
                {
                    if (string.IsNullOrEmpty(dt.Rows[a]["列名"].ToString()))//判定该列是否为空
                    { 
                       //  你要的操作内容
                    }
                }
     个人意见仅供参考
      

  21.   

                ShareDB.DataTable dtBook = new ShareDB.DataTable(ConnStr, "select (*) from book where booktime=@BookTime", new ParmStr("@BookTime", TextBox1.Text));
                for (int i = 0,iRowCount=dtBook.RowCount; i < iRowCount; i++)
                {
                    dtBook.SetPos(i);
                    if (dtBook.IsDBNull("BookTime"))
                    {
                        ShareDB.UpdateRow(ConnStr, "book", "KeyID=" + dtBook.GetInt("KeyID"), new ParmStr("BookTime", dtBook.GetStr("BookTime")));
                    }
                }
    上面是用我写的ShareDll.Net微架构实现的(包含高效率新方面读取数据+包含Json解析与生成类),准备发布。
      

  22.   

                ShareDB.DataTable dtBook = new ShareDB.DataTable(ConnStr, "select (*) from Book where booktime=@BookTime", new ParmStr("@BookTime", TextBox1.Text));
                int iColKeyID=dtBook.GetOrdinal("KeyID");
                int iColBookTime=dtBook.GetOrdinal("BookTime");
                for (int i = 0,iRowCount=dtBook.RowCount; i < iRowCount; i++)
                {
                    dtBook.SetPos(i);
                    if (dtBook.IsDBNull(iColBookTime))
                    {
                        ShareDB.UpdateRow(ConnStr, "book", "KeyID=" + dtBook.GetInt(iColKeyID), new ParmDate("BookTime", dtBook.GetStr(iColBookTime)));
                    }
                }
    这个纠正类型为ParmDate,并且优化效率更高一点使用列索引iColBookTime。
      

  23.   

    要update这一列中的空格应该怎么写
      

  24.   

    存入数据的话  SqlDataAdapter 有UPDATE这个属性 如果要后台操作这些空格的话做个判断就好if(dt.Rows[行号][列名]=="")
    {
       dt.Rows[..][..] ="";
    }LZ如果不明白的话建议去MSDN上去找找看 都是官方的API  上面写的比我这写的清楚多了