intput字符串格式不正确...将字符串转换为datetime时...错误? [英] intput string not in correct format...when converting a string to datetime...error?
问题描述
我正在尝试将参数传递给sql select查询。
出于某种原因,无论我尝试什么,我都会遇到同样的错误:
输入字符串不是核心格式
和建议:
将字符串转换为datetime时,解析在将每个变量放入datetime对象之前获取日期的字符串。
我很难知道该做什么。这是代码。
sql查询:
ALTER PROCEDURE SearchAllPlayers
(
@ personName ,
@ personPay ,
@ IDs
)
SELECT DISTINCT
person.personID,sports.sportsID,person.personName,person.personPay, sports.sport,
FROM sports INNER JOIN
person INNER JOIN
personSport ON personSport.personID = person.personID
INNER JOIN
personSport ON personSport.sportID = sports.sportsID
WHERE (person.personName LIKE ' %' + @ personName + ' %')
AND
(person.personPay> = @ personPay 或 0 = @personPay )
AND
(person.regDate> = {fn CURDATE()}
AND
(sports.sportID IN
( SELECT ID FROM dbo.fnSplitter( @ID )< span class =code-keyword> AS fnSplitter_1) OR @ IDs = 0 )
protected void Button1_Click( object sender,EventArgs e)
{
searchGrid();
}
受保护 void searchGrid()
{
SqlConnection conn = new SqlConnection(connSTR);
com = new SqlCommand();
conn.Open();
com.Connection = conn;
com.CommandText = SearchAllPlayers;
com.CommandType = CommandType.StoredProcedure;
string StringWithDelimiter = string .Empty;
for ( int i = 0 ; i < DropDownCheckBoxes1.Items.Count; i ++)
{
if (DropDownCheckBoxes1.Items [i] .Selected)
StringWithDelimiter + = DropDownCheckBoxes1.Items [i] .Value + ;跨度>;
}
com.Parameters.Add( @ personName,SqlDbType.VarChar).Value = personName.Text;
// 下一行是我遇到问题的地方
com.Parameters.Add( @ personPay,SqlDbType.Decimal).Value = ???? ??
com.Parameters.Add( @ IDs,SqlDbType.VarChar).Value = StringWithDelimiter;
sqlDa = new SqlDataAdapter(com);
dS = new DataSet();
sqlDa.Fill(dS);
conn.Close();
GridView1.DataSource = dS;
GridView1.DataBind();
}
到目前为止,我已经尝试了以下组合,所有这些都是有用的。
com.parameters.add(@ playerPay,sqldbtype.decimal).value = playerPayTextBox.Text;
这似乎不起作用,我得到以下错误:
将数据类型nvarchar转换为十进制时出错。
所以我试过这个:
com.parameters.add(@ playerPay,sqldbtype.decimal).value = playerPayTextBox。的ToString();
我得到同样的错误:
将数据类型nvarchar转换为十进制时出错。
这是有道理的,所以我试过:
com.parameters.add(@ playerPay,sqldbtype.decimal).value = decimal.parse(playerPayTextBox.tostring());
错误:输入字符串的格式不正确。 - 然后是日期时间的东西...
com.parameters.add(@ playerPay,sqldbtype.decimal).value = decimal.parse(playerPayTextBox.text) ;
错误:输入字符串的格式不正确。 - 然后是日期时间的东西...
com.parameters.add(@ playerPay,sqldbtype.decimal).value = convert.todecimal(playerPayTextBox.text) ;
错误:输入字符串的格式不正确。 - 然后是日期时间的东西...
com.parameters.add(@ playerPay,sqldbtype.decimal).value = convert.todecimal(playerPayTextBox.text) ;
错误:输入字符串的格式不正确。 - 然后是日期时间的东西...
不知道该怎么办
我假设在playerPayTextBox文本框中输入的值始终符合您设置的货币值,因此,您可以执行此操作
decimal.Parse(playerPayTextBox.text ,System.Globalization.NumberStyles.Currency);
如果输入的值可以是任何类似货币的值,那么你必须做更多的过滤。
您需要过滤
,和
和前面的任何其他货币符号
这可以通过多种方式完成,只要你只拿数字。
I am trying to pass a parameter to a sql select query.
for some reason, no matter what i try, i keep getting the same error:
input string not in the corect format
and the suggestion:
when converting a string to datetime, parse the string to take the date before putting each variable into the datetime object.
i'm stumped as to what to do. here's the code.
sql query:
ALTER PROCEDURE SearchAllPlayers
(
@personName,
@personPay,
@IDs
)
SELECT DISTINCT
person.personID, sports.sportsID, person.personName, person.personPay, sports.sport,
FROM sports INNER JOIN
person INNER JOIN
personSport ON personSport.personID = person.personID
INNER JOIN
personSport ON personSport.sportID = sports.sportsID
WHERE (person.personName LIKE '%' + @personName + '%')
AND
(person.personPay >= @personPay OR 0 = @personPay)
AND
(person.regDate >= { fn CURDATE() }
AND
(sports.sportID IN
(SELECT ID FROM dbo.fnSplitter(@IDs) AS fnSplitter_1) OR @IDs = 0)
protected void Button1_Click(object sender, EventArgs e)
{
searchGrid();
}
protected void searchGrid()
{
SqlConnection conn = new SqlConnection(connSTR);
com = new SqlCommand();
conn.Open();
com.Connection = conn;
com.CommandText = "SearchAllPlayers";
com.CommandType = CommandType.StoredProcedure;
string StringWithDelimiter = string.Empty;
for (int i = 0; i < DropDownCheckBoxes1.Items.Count; i++)
{
if (DropDownCheckBoxes1.Items[i].Selected)
StringWithDelimiter += DropDownCheckBoxes1.Items[i].Value + ";";
}
com.Parameters.Add("@personName", SqlDbType.VarChar).Value = personName.Text;
//next line is where I am having problems
com.Parameters.Add("@personPay", SqlDbType.Decimal).Value = ??????
com.Parameters.Add("@IDs", SqlDbType.VarChar).Value = StringWithDelimiter;
sqlDa = new SqlDataAdapter(com);
dS = new DataSet();
sqlDa.Fill(dS);
conn.Close();
GridView1.DataSource = dS;
GridView1.DataBind();
}
so far i've tried the following combinations, all to know avail.
com.parameters.add("@playerPay", sqldbtype.decimal).value = playerPayTextBox.Text;
this doesn't seem to work, i get the following error:
"Error converting data type nvarchar to decimal."
so then i tried this:
com.parameters.add("@playerPay", sqldbtype.decimal).value = playerPayTextBox.tostring();
i get the same error:
"Error converting data type nvarchar to decimal."
which makes sense, so then i tried:
com.parameters.add("@playerPay", sqldbtype.decimal).value = decimal.parse(playerPayTextBox.tostring());
error: "Input string was not in a correct format." - then the datetime thing...
com.parameters.add("@playerPay", sqldbtype.decimal).value = decimal.parse(playerPayTextBox.text);
error: "Input string was not in a correct format." - then the datetime thing...
com.parameters.add("@playerPay", sqldbtype.decimal).value = convert.todecimal(playerPayTextBox.text);
error: "Input string was not in a correct format." - then the datetime thing...
com.parameters.add("@playerPay", sqldbtype.decimal).value = convert.todecimal(playerPayTextBox.text);
error: "Input string was not in a correct format." - then the datetime thing...
not sure what to do
I'm assuming the value entered in playerPayTextBox textbox always conform to a currency value you have set so, you can do this
decimal.Parse(playerPayTextBox.text, System.Globalization.NumberStyles.Currency);
If the value entered can be anything resembling a currency then you have to do more filtering.
You need to filter
"," and "
" and any other currency symbol at front
This can be done in variety of ways as long as you only take the numbers.
这篇关于intput字符串格式不正确...将字符串转换为datetime时...错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!