简体   繁体   English

输入字符串格式不正确...将字符串转换为datetime时...错误?

[英]input string not in correct format…when converting a string to datetime…error?

I am trying to pass a parameter to a sql select query. 我试图将参数传递给sql选择查询。

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. 和建议:在将字符串转换为datetime时,解析字符串以在将每个变量放入datetime对象之前获取日期。

i'm stumped as to what to do. 我很难过该做什么。 here's the code. 这是代码。

sql query: 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 OR 0 = @personPay)
              AND
             (person.regDate >= { fn CURDATE() }
              AND
             (sports.sportID IN
                    (SELECT   ID FROM dbo.fnSplitter(@IDs) AS fnSplitter_1) OR @IDs = 0)

codebehind: 代码隐藏:

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;

 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." 这似乎不起作用,我得到以下错误:“将数据类型nvarchar转换为十进制时出错。”

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." 我得到相同的错误:“将数据类型nvarchar转换为十进制时出错。”

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 不知道该怎么办

Make sure your input string is using decimal point symbol for your current culture!. 确保您的输入字符串使用小数点符号表示您当前的文化! If your input string does not match these rules, decimal.Parse throws the Input string was not in a correct format exception 如果您的输入字符串与这些规则不匹配,则decimal.Parse会抛出Input字符串格式不正确的异常

If you for instance use the invariant culture: 例如,如果您使用不变文化:

decimal.Parse("17.5", CultureInfo.InvariantCulture)

The dot will be the accepted decimal point. 该点将是可接受的小数点。

You can read more about it over at MSDN 您可以在MSDN上阅读有关它的更多信息

There they also mention that the decimal.TryParse method is better suited for invariant culture parsing 他们还提到decimal.TryParse方法更适合不变的文化解析

Also (obviously), make sure your input string is not null, empty, or containing any non-numerical characters (except for the one, optional, dot or comma, depending on your culture) (显然),确保您的输入字符串不为空,空或包含任何非数字字符(除了一个,可选,点或逗号,取决于您的文化)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM