Skip to main content
 首页 » 编程设计

c#之输入字符串格式不正确...将字符串转换为日期时间时...错误

2024年02月13日25rubylouvre

我正在尝试将参数传递给 sql 选择查询。

出于某种原因,无论我尝试什么,我都会不断收到相同的错误: 输入的字符串格式不正确

和建议: 将字符串转换为日期时间时,先解析字符串以获取日期,然后再将每个变量放入日期时间对象。

我不知道该怎么办。这是代码。

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) 

代码隐藏:

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; 

这似乎不起作用,我收到以下错误: “将数据类型 nvarchar 转换为 decimal 时出错。”

然后我尝试了这个:

com.parameters.add("@playerPay", sqldbtype.decimal).value = playerPayTextBox.tostring(); 

我得到同样的错误: “将数据类型 nvarchar 转换为 decimal 时出错。”

这是有道理的,所以我尝试了:

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); 

错误:“输入的字符串格式不正确。” - 然后是日期时间……

不知道该怎么办

请您参考如下方法:

确保您的输入字符串使用的是您当前文化的小数点符号!。如果您的输入字符串不符合这些规则,decimal.Parse 会抛出 Input string was not in a correct format 异常

例如,如果您使用不变文化:

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

点将是可接受的小数点。

您可以在 MSDN 阅读更多相关信息

他们还提到 decimal.TryParse 方法更适合不变文化解析

此外(显然),请确保您的输入字符串不为 null、空或包含任何非数字字符(除了一个、可选的、点或逗号,具体取决于您的文化)