简单的动态SQL语句求助 float flo = 0;初始化一个值 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 试一下初始化时赋值成null,等待中 如果我给它赋值的话,那么值就会在存入数据库,到时用报表打印就会打印出该值,我是想如果有填写TextBox2的话就打印而没有填写就不打印该值。 harisonh2l(h2l):我试过的,提示说不能将float转化为null型 float flo = 0;初始化一个值 string mysql,str;float flo;str=TextBox1.Text.Trim();if(TextBox2.Text.Trim()!=""){ try{ flo=Convert.ToSingle(TextBox2.Text.Trim()); } catch{ Message.Show(....); return; }}mysql="Insert Into Table1(F_String,F_Float)Values('"+str+"',"+flo+")"提示此句错误:使用了未赋值的局部变量“flo”。我的要求是flo并非是必填项目,请教高手们要如何解决? 在提出数据库前可以先做判断的,如果是null则可以采取其他措施 赋初值为0在写入数据库之前判断如果为0 SQL语句改为insert into table1(f_string) values ('"+str+"')" to harisonh2l(h2l):现在问题是不能赋null给flo呀 string mysql,str;float flo = 0;str=TextBox1.Text.Trim();if(TextBox2.Text.Trim()!=""){ try{ flo=Convert.ToSingle(TextBox2.Text.Trim()); } catch{ Message.Show(....); return; }}if(flo == 0){mysql="Insert Into Table1(F_String)Values('"+str+"')"}else{mysql="Insert Into Table1(F_String,F_Float)Values('"+str+"',"+flo+")"} angxain(卖女孩的小火财):你这个方法我知道。可是我真正的程序要比这个复杂的多,表中的字段就有60个左右,其中大部分为可填可不填的,用你这种方法几乎是不可行的。 string sInsertKey = "Insert into Table1(";string sInsertValue = " Values(";if (str != ""){ sInsertKey += "F_String,"; sInsertValue += "'"+str+"'";}if (flo != 0){ sInsertKey += "F_Float,"; sInsertValue += "'"+flo+"'";}...sInsertKey += ") ";sInsertValue += ")";string sSQL = sInsertKey + sInsertValue;这样行吗?~~呵呵 if (flo != 0){ sInsertKey += "F_Float,"; sInsertValue += "'"+flo+"',";<-----刚才的少了个“,”呵呵} 倒,忘了还要去掉最后一个“,”呵呵sInsertKey = sInsertKey.TrimEnd(',') + ") ";sInsertValue = sInsertValue.TrimEnd(',') + ")";string sSQL = sInsertKey + sInsertValue; ulysis6(令狐葱)的方法可以接受!谢谢各位大侠的帮助! 新手学习:父窗体与子窗体 问一个Winform在父窗体中打开一个新窗体的问题 让选定的歌曲顺序播放 请问有看得懂易语言的吗,帮忙把这段代码改成C#,谢谢。 有关用Visual Studio调试多线程程序的问题 关于VAssistX换行加;问题 客户端如何获取IIS里面虚拟目录的路径?或者如何将XML的文件生成到客户端的system32目录? 数据库问题,谢谢帮助. C#入门问题!!!! 小妹给大家请安了!呵呵~~~~~~~~~~~~~~~``` 有在武汉的用C#的程序员吗? DataGrid中的ItemCreated事件中写入数据项目?????
初始化一个值
float flo;str=TextBox1.Text.Trim();
if(TextBox2.Text.Trim()!="")
{
try{
flo=Convert.ToSingle(TextBox2.Text.Trim());
}
catch{
Message.Show(....);
return;
}
}mysql="Insert Into Table1(F_String,F_Float)Values('"+str+"',"+flo+")"
提示此句错误:使用了未赋值的局部变量“flo”。
我的要求是flo并非是必填项目,请教高手们要如何解决?
现在问题是不能赋null给flo呀
float flo = 0;str=TextBox1.Text.Trim();
if(TextBox2.Text.Trim()!="")
{
try{
flo=Convert.ToSingle(TextBox2.Text.Trim());
}
catch{
Message.Show(....);
return;
}
}
if(flo == 0)
{
mysql="Insert Into Table1(F_String)Values('"+str+"')"
}
else
{
mysql="Insert Into Table1(F_String,F_Float)Values('"+str+"',"+flo+")"
}
你这个方法我知道。可是我真正的程序要比这个复杂的多,表中的字段就有60个左右,其中大部分为可填可不填的,用你这种方法几乎是不可行的。
string sInsertValue = " Values(";
if (str != "")
{
sInsertKey += "F_String,";
sInsertValue += "'"+str+"'";
}
if (flo != 0)
{
sInsertKey += "F_Float,";
sInsertValue += "'"+flo+"'";
}
.
.
.
sInsertKey += ") ";
sInsertValue += ")";
string sSQL = sInsertKey + sInsertValue;
这样行吗?~~呵呵
{
sInsertKey += "F_Float,";
sInsertValue += "'"+flo+"',";<-----刚才的少了个“,”呵呵
}
sInsertKey = sInsertKey.TrimEnd(',') + ") ";
sInsertValue = sInsertValue.TrimEnd(',') + ")";
string sSQL = sInsertKey + sInsertValue;
谢谢各位大侠的帮助!