输入字符串格式不正确 [英] Input string not in correct format

查看:88
本文介绍了输入字符串格式不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我在插入字符串变量时遇到问题:


Dim strSQL As String =" INSERT INTO tblContactForm1(txtName,txtCompany,

txtPhone,txtEmail,txtComment,chkGrower,chkProduceDealer,

txtOtherCustType,chkStandardBags,chkCustomBags,txtOtherBags)" + _


" VALUES(''" + txtName.Text +"'',''" + txtCompany.Text +"'',''" + txtPhone.Text

+"'',''" + txtEmail.Text +"'',''" + txtComment.Text +"'',''" +

chkGrower.Checked +"'',''" + chkProduceDealer.Checked + _


"'',''" + txtOtherCustType.Text +"'',''" + chkStandardBags.Checked +"'',''" +

chkCustomBags.Checked +"'',''" + txtOtherBags.Text +"'');"

我在Visual Web Developer Webform中运行此过程。我有点想b / b
认为这个问题可能是第二行结束时的连续字符(+ _):如果该行没有拆分那么它会读,


chkProduceDealer.Checked +"'',''" + txtOtherCustType.Text


所以我在现有的concatination旁边添加了下划线而不是

新的符号对,+ _。我尝试在其他地方打破线路,但是

没有用。


感谢您的帮助。


上帝保佑,


Mark A. Sam

解决方案

如果我怀疑,线条被延续分裂的原因

字符纯粹是为了节省你不得不侧身滚动看看你写的是什么
,那么我的建议就是继续在你正常工作之前,至少需要



你有没有打开Option Strict?我怀疑你没有,否则你将获得一些编译器错误和/或警告。


数据库表中列的名称与表单上控件名称的

名称完全相同?


数据库表列的数据类型是否应该声明为什么?


结果字符串的值是多少? CLUE:使用Console.Writeline

看到它。


在VALUES子句中,我希望chkGrower列的值为

要么是......,''真'',......还是......,''错'',......这同样适用于

其他chk。 ..列,其中syuspect是CheckBoxes的来源。


如果数据库表中的相应列是

没有声明为什么我会感到惊讶你的数据库引擎

支持的boolean的味道。


如果是这种情况,那么传递字符串''True''或''False''是你的

问题。你需要传递真或假布尔值。


" Mark A. Sam" < MS ** @ Plan-It-Earth.Net>在消息中写道

news:u9 ************** @ TK2MSFTNGP12.phx.gbl ...

你好,
我遇到了插入字符串变量的问题:

Dim strSQL As String =" INSERT INTO tblContactForm1(txtName,txtCompany,
txtPhone,txtEmail,txtComment, chkGrower,chkProduceDealer,
txtOtherCustType,chkStandardBags,chkCustomBags,txtOtherBags)" + _

" VALUES(''" + txtName.Text +"'',''" + txtCompany.Text +"'',''" +
txtPhone.Text +"'',''" + txtEmail.Text +"'',''" + txtComment.Text +"'',''" +
chkGrower.Checked +"'',''" + chkProduceDealer.Checked + _

"'',''" + txtOtherCustType.Text +"'',''" + chkStandardBags。选中+"'',''" +
chkCustomBags.Checked +"'',''" + txtOtherBags.Text +"'');"

我在Visual Web Developer Webform中运行此过程。我很善意认为这个问题可能是第二行末尾的连续字符(+ _):如果这条线没有分开就会读到,

chkProduceDealer.Checked +"'',''" + txtOtherCustType.Text

所以我在现有的concatination旁边添加了下划线,而不是一对新的符号+ _。我尝试在其他地方打破这条线,但是
没有用。

感谢您的帮助。

上帝保佑,



Hello Stephany,


我在发布之后想到了这个,所以我将它作为单行运行并且

仍有问题。

Dim strSQL As String =" INSERT INTO tblContactForm1(txtName,txtCompany,

txtPhone,txtEmail,txtComment,chkGrower,chkProduceDealer,

txtOtherCustType,chkStandardBags,chkCustomBags,txtOtherBags)VALUES(''" +

txtName.Text +"'',''" + txtCompany.Text +"'',''" + txtPhone.Text +"'',''" +

txtEmail.Text +"'',''" + txtComment.Text +"'',''" + chkGrower.Checked +"'',''"

+ chkProduceDealer.Checked +"'',''" + txtOtherCustType.Text +&quo t;'',''" +

chkStandardBags.Checked +"'',''" + chkCustomBags.Checked +"'',''" +

txtOtherBags.Text +"'');"


我没有看到问题。当我使用

前两个字段txtName和txtCompany进行测试时,这种格式工作正常。我没有看到任何语法错误,

虽然他们可能在那里。


如果我可以的另一个问题。我应该删除

布尔字段的单引号,例如


+"'',''" + chkStandardBags.Checked +"'',''" +


谢谢。


Mark





你不应该在第一个地方连接你的查询,因为这个方法出现了一堆

安全问题,而且它也不是很可读

代码。而是使用框架为您提供的内容并参数化您的

查询并使用正确定义的数据类型发送值,例如

示例:


我在这里使用C#,因为这是我习惯的,但同样适用于

VB.NET,我也缩短了你的查询只是为了这个例子:


string strSQL =" INSERT INTO tblContactForm1(txtName,txtCompany)VALUES

(@txtName,@ txtCompany)" ;;


SqlConnection objconn = new SqlConnection(sConnStr);

objconn.Open();


SqlCommand objcommand = new SqlCommand(strSQL,objconn) ;


//设置参数值

objcommand.Parameters.Add("(@ txtName",SqlDbType.VarChar).Value =

txtName.Text;

objcommand.Parameters.Add("(@ txtCompany",SqlDbType.VarChar).Value =

txtCompany.Text;


// ...其余代码


PL。

Hello,

I am having a problem with imputting into a string variable:

Dim strSQL As String = "INSERT INTO tblContactForm1 (txtName, txtCompany,
txtPhone, txtEmail, txtComment, chkGrower, chkProduceDealer,
txtOtherCustType, chkStandardBags, chkCustomBags,txtOtherBags) " + _

"VALUES (''" + txtName.Text + "'',''" + txtCompany.Text + "'',''" + txtPhone.Text
+ "'',''" + txtEmail.Text + "'',''" + txtComment.Text + "'',''" +
chkGrower.Checked + "'',''" + chkProduceDealer.Checked + _

"'',''" + txtOtherCustType.Text + "'',''" + chkStandardBags.Checked + "'',''" +
chkCustomBags.Checked + "'',''" + txtOtherBags.Text + "'');"
I am running this procedure in a Visual Web Developer Webform. I am kind of
thinking the problem may be the contiuation characters (+ _) on the end of
the second line: If the line wasn''t split there it would read,

chkProduceDealer.Checked + "'',''" + txtOtherCustType.Text

So I added the underscore next to an existing concatination rather than a
new pair of symbols, + _. I tried breaking the line somewhere else, but
that didn''t work.

Thanks for any help.

God Bless,

Mark A. Sam

解决方案

If, as I suspect, the reason for the lines being split with the continuation
character is purely to save you having to scroll sideways to see what you
have written, then my advice is to take the continuation characters out, at
least until you have it working properly.

Have you got Option Strict turned on? I suspect you haven''t, otherwise you
would get some compiler errors and/or warnings.

Are the names on the columns in your database table really the same as the
names of the controls name on your form?

Are the datatypes of the database table columns declared as they should be?

What is the value of the resulting string? CLUE: Use Console.Writeline to
see it.

In the VALUES clause, I would expect that the value for the chkGrower column
to be either ...,''True'',... or ...,''False'',... and the same applies to the
other chk... columns, the source of which syuspect to be CheckBoxes.

I would be surprised if the corresponding columns in the database table are
not declared as whatever flavour of boolean that your database engine
supports.

If this is the case then passing the string ''True'' or ''False'' is your
problem. You need to be passing True or False boolean values.

"Mark A. Sam" <ms**@Plan-It-Earth.Net> wrote in message
news:u9**************@TK2MSFTNGP12.phx.gbl...

Hello,

I am having a problem with imputting into a string variable:

Dim strSQL As String = "INSERT INTO tblContactForm1 (txtName, txtCompany,
txtPhone, txtEmail, txtComment, chkGrower, chkProduceDealer,
txtOtherCustType, chkStandardBags, chkCustomBags,txtOtherBags) " + _

"VALUES (''" + txtName.Text + "'',''" + txtCompany.Text + "'',''" +
txtPhone.Text + "'',''" + txtEmail.Text + "'',''" + txtComment.Text + "'',''" +
chkGrower.Checked + "'',''" + chkProduceDealer.Checked + _

"'',''" + txtOtherCustType.Text + "'',''" + chkStandardBags.Checked + "'',''" +
chkCustomBags.Checked + "'',''" + txtOtherBags.Text + "'');"
I am running this procedure in a Visual Web Developer Webform. I am kind
of thinking the problem may be the contiuation characters (+ _) on the end
of the second line: If the line wasn''t split there it would read,

chkProduceDealer.Checked + "'',''" + txtOtherCustType.Text

So I added the underscore next to an existing concatination rather than a
new pair of symbols, + _. I tried breaking the line somewhere else, but
that didn''t work.

Thanks for any help.

God Bless,

Mark A. Sam



Hello Stephany,

I thought about that after I posted this, so I ran it as one single line and
still have the issue.

Dim strSQL As String = "INSERT INTO tblContactForm1 (txtName, txtCompany,
txtPhone, txtEmail, txtComment, chkGrower, chkProduceDealer,
txtOtherCustType, chkStandardBags, chkCustomBags,txtOtherBags) VALUES (''" +
txtName.Text + "'',''" + txtCompany.Text + "'',''" + txtPhone.Text + "'',''" +
txtEmail.Text + "'',''" + txtComment.Text + "'',''" + chkGrower.Checked + "'',''"
+ chkProduceDealer.Checked + "'',''" + txtOtherCustType.Text + "'',''" +
chkStandardBags.Checked + "'',''" + chkCustomBags.Checked + "'',''" +
txtOtherBags.Text + "'');"

I don''t see the problem. This format worked fine when I tested it with the
first two field, txtName and txtCompany. I don''t see any syntax errors,
though they could be there.

Another question if I may. Should I remove the single quote surrouding the
boolean fields, like

+ "'',''" + chkStandardBags.Checked + "'',''" +

Thank you.

Mark




You shouldnt concatenate your queries in the first palce since a bunch of
security issues arise with this method and it is also not very readable
code. Instead use what the framework provides you with and parameterize your
query and send in the values with the datatypes properly defined, for
example:

I''m using C# here since that is what I''m used to but the same applies to
VB.NET, I have also shortened your query just for this example:

string strSQL= "INSERT INTO tblContactForm1 (txtName, txtCompany) VALUES
(@txtName, @txtCompany)";

SqlConnection objconn = new SqlConnection(sConnStr);
objconn.Open();

SqlCommand objcommand = new SqlCommand(strSQL,objconn);

// Set the parameter values
objcommand.Parameters.Add("(@txtName", SqlDbType.VarChar).Value =
txtName.Text;
objcommand.Parameters.Add("(@txtCompany", SqlDbType.VarChar).Value =
txtCompany.Text;

// ... rest of code

PL.


这篇关于输入字符串格式不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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