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的值写入这一列
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的值写入这一列
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();
}
例如: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()+ ");
最后,执行入库操作。试试看,惊喜等着你!
sql=string.Format(sql,替换{0}的字符串,替换{1}的字符串,替换{2}的字符串);
declare @booktime string
select @booktime=booktime from book where 条件
这里是不是还要加上declare T1
declare T2
select T1
select T2
同时获得多个列应该怎么写?
{
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后面没什么条件所以就没写,但是值还是没有写进数据库里,这个代码还有哪些问题
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()))//判定该列是否为空
{
// 你要的操作内容
}
}
个人意见仅供参考
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解析与生成类),准备发布。
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。
{
dt.Rows[..][..] ="";
}LZ如果不明白的话建议去MSDN上去找找看 都是官方的API 上面写的比我这写的清楚多了