ADO.NEt保存空值 [英] ADO.NEt saving null values

查看:70
本文介绍了ADO.NEt保存空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我创建了一个访问数据库,我正在使用ado.net来访问它。现在我是

试图创建一个更新语句,但是我有一些错误,所以

字段是空的。


这里是我的判断,从屏幕上读取一组文本框并尝试将
保存起来。


string sSql;

OleDbConnection conn = new OleDbConnection(Form1.Dbase);

sSql =" Update Users Set";

sSql = sSql +" Usr_login = \"" + textBox1.Text.Trim()+" \"," ;;

sSql = sSql +" Usr_name = \"" + textBox2.Text.Trim()+" \"," ;;

sSql = sSql +" Usr_desg = \"" + textBox3.Text.Trim()+" \"," ;;

sSql = sSql +" Usr_mail = \"" + textBox4.Text.Trim()+" \"," ;;

sSql = sSql +" Usr_mobile = \"" + textBox5.Text +" \"," ;;

sSql = sSql +" Usr_disb =" + checkBox2.Checked.ToString();

sSql = sSql +" Users.usr_code =" ;;

sSql = sSql + pbusr;

//

//返回;

conn.Open();

OleDbCommand cmd = new OleDbCommand(sSql,conn);

cmd.ExecuteNonQuery();

conn .Close();


这样可以正常工作,直到所有文本框都包含某些内容。一旦我打开其中一个文本框,系统就会出错。有什么想法吗?

Hi,

I created an access database and I am using ado.net to access it. Now I am
trying to create an update statment but I am having some error when so
fields are empty.

Here is my statment which read a set of textboxes from a screen and tries to
save them.

string sSql;
OleDbConnection conn = new OleDbConnection(Form1.Dbase);
sSql = "Update Users Set ";
sSql = sSql + "Usr_login = \"" + textBox1.Text.Trim() + "\",";
sSql = sSql + "Usr_name = \"" + textBox2.Text.Trim() + "\",";
sSql = sSql + "Usr_desg = \"" + textBox3.Text.Trim() + "\",";
sSql = sSql + "Usr_mail = \"" + textBox4.Text.Trim() + "\",";
sSql = sSql + "Usr_mobile = \"" + textBox5.Text + "\",";
sSql = sSql + "Usr_disb = " + checkBox2.Checked.ToString();
sSql = sSql + " where Users.usr_code =";
sSql = sSql + pbusr;
//
//return;
conn.Open();
OleDbCommand cmd = new OleDbCommand(sSql,conn);
cmd.ExecuteNonQuery();
conn.Close();

This works fine until all the textboxes contain something. As soon as I
empty one of the textboxes the system gives an error. Any idea?

推荐答案

你好伊万,


你的数据库可能不接受空值(空字符串也是)。

无论如何,你应该使用参数化的sql命令而不是动态的

连接字符串。

BTW,你接收到什么错误?


-

Miha Markic - RightHand .NET咨询&开发

miha at rthand com
www.rhand.com


" Ivan Sammut" < SA ***** @ nextgen.net.mt>在消息中写道

新闻:ei ************** @ TK2MSFTNGP12.phx.gbl ...
Hi Ivan,

You database probably doesn''t accept null values (empty strings also).
Anyway, you should use parametrised sql commands rather than dynamically
concatenated strings.
BTW, what error are you receiving?

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rhand.com

"Ivan Sammut" <sa*****@nextgen.net.mt> wrote in message
news:ei**************@TK2MSFTNGP12.phx.gbl...

我创建了一个访问数据库,我正在使用ado.net来访问它。现在我正在尝试创建一个更新语句,但是当我的
字段为空时我有一些错误。

这是我从屏幕读取一组文本框的语句并尝试
来保存它们。

字符串sSql;
OleDbConnection conn = new OleDbConnection(Form1.Dbase);
sSql =" Update Users Set" ;;
sSql = sSql +" Usr_login = \"" + textBox1.Text.Trim()+" \"," ;;
sSql = sSql +" Usr_name = \"" + textBox2.Text.Trim()+" \"," ;;
sSql = sSql +" Usr_desg = \"" + textBox3.Text.Trim()+" \"," ;;
sSql = sSql +" Usr_mail = \"" + textBox4.Text.Trim()+" \"," ;;
sSql = sSql +" Usr_mobile = \"" + textBox5.Text +" \"," ;;
sSql = sSql +" Usr_disb =" + checkBox2.Checked.ToString();
sSql = sSql +" Users.usr_code =" ;;
sSql = sSql + pbusr;
//
// return;
conn.Open();
OleDbCommand cmd = new OleDbCommand(sSql,conn);
cmd.ExecuteNonQuery();
conn.Close();

这一切正常,直到所有文本框都包含某些内容。一旦我清空其中一个文本框,系统就会出错。有什么想法吗?
Hi,

I created an access database and I am using ado.net to access it. Now I am
trying to create an update statment but I am having some error when so
fields are empty.

Here is my statment which read a set of textboxes from a screen and tries to save them.

string sSql;
OleDbConnection conn = new OleDbConnection(Form1.Dbase);
sSql = "Update Users Set ";
sSql = sSql + "Usr_login = \"" + textBox1.Text.Trim() + "\",";
sSql = sSql + "Usr_name = \"" + textBox2.Text.Trim() + "\",";
sSql = sSql + "Usr_desg = \"" + textBox3.Text.Trim() + "\",";
sSql = sSql + "Usr_mail = \"" + textBox4.Text.Trim() + "\",";
sSql = sSql + "Usr_mobile = \"" + textBox5.Text + "\",";
sSql = sSql + "Usr_disb = " + checkBox2.Checked.ToString();
sSql = sSql + " where Users.usr_code =";
sSql = sSql + pbusr;
//
//return;
conn.Open();
OleDbCommand cmd = new OleDbCommand(sSql,conn);
cmd.ExecuteNonQuery();
conn.Close();

This works fine until all the textboxes contain something. As soon as I
empty one of the textboxes the system gives an error. Any idea?



请给我一个参数化sql命令的例子。


对不起,如果这是一个愚蠢的问题,但我仍然是一个菜鸟


谢谢

Ivan Sammut

" ; Miha Markic < miha at rthand com>在消息中写道

news:u4 ************** @ TK2MSFTNGP12.phx.gbl ...
Can u pls give me an example of a parametrised sql commands .

Sorry if it''s a stupid question, but i''m still a rookie

thanks
Ivan Sammut
"Miha Markic" <miha at rthand com> wrote in message
news:u4**************@TK2MSFTNGP12.phx.gbl...
Hi Ivan,

你的数据库可能不接受空值(也是空字符串)。
无论如何,你应该使用参数化的sql命令而不是动态串联的字符串。
BTW,你收到了什么错误?

-
Miha Markic - RightHand .NET咨询&开发
miha at rthand com
www.rhand.com

Ivan Sammut < SA ***** @ nextgen.net.mt>在消息中写道
新闻:ei ************** @ TK2MSFTNGP12.phx.gbl ...
Hi Ivan,

You database probably doesn''t accept null values (empty strings also).
Anyway, you should use parametrised sql commands rather than dynamically
concatenated strings.
BTW, what error are you receiving?

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rhand.com

"Ivan Sammut" <sa*****@nextgen.net.mt> wrote in message
news:ei**************@TK2MSFTNGP12.phx.gbl...

我创建了一个访问数据库,我正在使用ado.net来访问它。现在我想b $ b我正在尝试创建一个更新语句,但是当我的
字段为空时我有一些错误。

这是我从屏幕读取一组文本框的语句并且
Hi,

I created an access database and I am using ado.net to access it. Now I am trying to create an update statment but I am having some error when so
fields are empty.

Here is my statment which read a set of textboxes from a screen and


尝试

保存它们。

字符串sSql;
OleDbConnection conn = new OleDbConnection(Form1.Dbase);
sSql =" Update Users Set";
sSql = sSql +" Usr_login = \"" + textBox1.Text.Trim()+" \"," ;;
sSql = sSql +" Usr_name = \"" + textBox2.Text.Trim()+" \"," ;;
sSql = sSql +" Usr_desg = \"" + textBox3.Text.Trim()+" \"," ;;
sSql = sSql +" Usr_mail = \"" + textBox4.Text.Trim()+" \"," ;;
sSql = sSql +" Usr_mobile = \"" + textBox5.Text +" \"," ;;
sSql = sSql +" Usr_disb =" + checkBox2.Checked.ToString();
sSql = sSql +" Users.usr_code =" ;;
sSql = sSql + pbusr;
//
// return;
conn.Open();
OleDbCommand cmd = new OleDbCommand(sSql,conn);
cmd.ExecuteNonQuery();
conn.Close();

这一切正常,直到所有文本框都包含某些内容。一旦我清空其中一个文本框,系统就会出错。有什么想法吗?
save them.

string sSql;
OleDbConnection conn = new OleDbConnection(Form1.Dbase);
sSql = "Update Users Set ";
sSql = sSql + "Usr_login = \"" + textBox1.Text.Trim() + "\",";
sSql = sSql + "Usr_name = \"" + textBox2.Text.Trim() + "\",";
sSql = sSql + "Usr_desg = \"" + textBox3.Text.Trim() + "\",";
sSql = sSql + "Usr_mail = \"" + textBox4.Text.Trim() + "\",";
sSql = sSql + "Usr_mobile = \"" + textBox5.Text + "\",";
sSql = sSql + "Usr_disb = " + checkBox2.Checked.ToString();
sSql = sSql + " where Users.usr_code =";
sSql = sSql + pbusr;
//
//return;
conn.Open();
OleDbCommand cmd = new OleDbCommand(sSql,conn);
cmd.ExecuteNonQuery();
conn.Close();

This works fine until all the textboxes contain something. As soon as I
empty one of the textboxes the system gives an error. Any idea?




Ivan,


简单的例子:

而不是


[C#]

sqlcommand.CommandText =" SELECT * FROM Category WHERE CategoryID = 1" ;;


你会用

sqlcommand.commandtext =" SELECT * FROM Category WHERE CategoryID =

@CategoryID" ;;

sqlcommand.Parameters.Add(" @ CategoryID",SqlDbType.Int);

sqlcommand.Parameters [" @ CategoryID"]。Value = 1;


所以,不要将参数值放入sql命令,而是使用

参数。

如果你需要执行更多,这种方式会更快相同的命令具有不同的

参数(服务器分析命令只有一次)和更安全的方式。


.net帮助中有大量信息。

开始时你可以检查

使用带DataAdapter的参数

帮助主题。


-

Miha Markic - RightHand .NET咨询&开发

miha at rthand com
www.rhand.com

" Ivan Sammut" < SA ***** @ nextgen.net.mt>在消息中写道

news:%2 ****************** @ TK2MSFTNGP12.phx.gbl ...
Ivan,

Simple example:
instead of

[C#]
sqlcommand.CommandText = "SELECT * FROM Category WHERE CategoryID = 1";

you would use
sqlcommand.commandtext = "SELECT * FROM Category WHERE CategoryID =
@CategoryID ";
sqlcommand.Parameters.Add("@CategoryID", SqlDbType.Int);
sqlcommand.Parameters["@CategoryID"].Value = 1;

So, instead of putting parameter values into sql command, you use
parameters.
This way is faster if you need to execute more same commands with different
parameters (server parses command only once) and way more secure.

There is plenty of info in .net help.
For beginning you might check
Using Parameters with a DataAdapter
help topic.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rhand.com
"Ivan Sammut" <sa*****@nextgen.net.mt> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
你能不能给我一个参数化的sql命令的例子。

抱歉,如果这是一个愚蠢的问题,但我仍然是一个菜鸟

谢谢 Ivan Sammut
Miha Markic < miha at rthand com>在消息中写道
新闻:u4 ************** @ TK2MSFTNGP12.phx.gbl ...
Can u pls give me an example of a parametrised sql commands .

Sorry if it''s a stupid question, but i''m still a rookie

thanks
Ivan Sammut
"Miha Markic" <miha at rthand com> wrote in message
news:u4**************@TK2MSFTNGP12.phx.gbl...
嗨伊万,
无论如何,你应该使用参数化的sql命令而不是动态串联的字符串。
BTW,你有什么错误接收?

- Miha Markic - RightHand .NET咨询&开发
miha at rthand com
www.rhand.com

Ivan Sammut < SA ***** @ nextgen.net.mt>在消息中写道
新闻:ei ************** @ TK2MSFTNGP12.phx.gbl ...
Hi Ivan,

You database probably doesn''t accept null values (empty strings also).
Anyway, you should use parametrised sql commands rather than dynamically
concatenated strings.
BTW, what error are you receiving?

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rhand.com

"Ivan Sammut" <sa*****@nextgen.net.mt> wrote in message
news:ei**************@TK2MSFTNGP12.phx.gbl...

我创建了一个访问数据库,我正在使用ado.net来访问它。现在
Hi,

I created an access database and I am using ado.net to access it. Now I
am
尝试创建更新语句但是我遇到了一些错误
字段空的。

这是我的判断,从屏幕上读取一组文本框,
trying to create an update statment but I am having some error when so
fields are empty.

Here is my statment which read a set of textboxes from a screen and


尝试

保存它们。

字符串sSql;
OleDbConnection conn = new OleDbConnection(Form1.Dbase);
sSql =" Update Users Set"; br /> sSql = sSql +" Usr_login = \"" + textBox1.Text.Trim()+" \"," ;;
sSql = sSql +" Usr_name = \"" + textBox2.Text.Trim()+" \"," ;;
sSql = sSql +" Usr_desg = \"" + textBox3.Text.Trim()+" \"," ;;
sSql = sSql +" Usr_mail = \"" + textBox4.Text.Trim()+" \"," ;;
sSql = sSql +" Usr_mobile = \"" + textBox5.Text +" \"," ;;
sSql = sSql +" Usr_disb =" + checkBox2.Checked.ToString();
sSql = sSql +" Users.usr_code =" ;;
sSql = sSql + pbusr;
//
// return;
conn.Open();
OleDbCommand cmd = new OleDbCommand(sSql,conn);
cmd.ExecuteNonQuery();
conn.Close();

这一切正常,直到所有文本框都包含某些内容。一旦
save them.

string sSql;
OleDbConnection conn = new OleDbConnection(Form1.Dbase);
sSql = "Update Users Set ";
sSql = sSql + "Usr_login = \"" + textBox1.Text.Trim() + "\",";
sSql = sSql + "Usr_name = \"" + textBox2.Text.Trim() + "\",";
sSql = sSql + "Usr_desg = \"" + textBox3.Text.Trim() + "\",";
sSql = sSql + "Usr_mail = \"" + textBox4.Text.Trim() + "\",";
sSql = sSql + "Usr_mobile = \"" + textBox5.Text + "\",";
sSql = sSql + "Usr_disb = " + checkBox2.Checked.ToString();
sSql = sSql + " where Users.usr_code =";
sSql = sSql + pbusr;
//
//return;
conn.Open();
OleDbCommand cmd = new OleDbCommand(sSql,conn);
cmd.ExecuteNonQuery();
conn.Close();

This works fine until all the textboxes contain something. As soon as



我清空其中一个文本框,系统就会出错。有什么想法吗?


I empty one of the textboxes the system gives an error. Any idea?





这篇关于ADO.NEt保存空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆