C# 使用开关状态调整文本框的大小
嗨,我有下面的代码,我需要在我的应用程序中设置一个文本框的最大长度。代码看起来还可以,但不起作用。有人知道问题出在哪里吗C# 使用开关状态调整文本框的大小,c#,winforms,C#,Winforms,嗨,我有下面的代码,我需要在我的应用程序中设置一个文本框的最大长度。代码看起来还可以,但不起作用。有人知道问题出在哪里吗 private void cbType_SelectionChanged(object sender, SelectionChangedEventArgs e) { string constring = "Data Source=.;Initial Catalog=db.MDF;Integrated Security=True";
private void cbType_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string constring = "Data Source=.;Initial Catalog=db.MDF;Integrated Security=True";
string Query = "select * from RePriorities where Priority='" + cbType.SelectedItem.ToString() + "' ;";
SqlConnection conDataBase = new SqlConnection(constring);
SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase);
SqlDataReader myReader;
try
{
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
string sType = myReader.ToString();
switch (sType)
{
case "Low": txtDesc.MaxLength = 5; break;
case "Medium": txtDesc.MaxLength = 10; break;
case "High": txtDesc.MaxLength = 1; break;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
打开SqlDataReader后,需要调用Read方法将内部记录指针放置到第一条记录。只有在这之后,您才能从读取器中提取值
myReader = cmdDataBase.ExecuteReader();
if(myReader.Read())
{
string sType = myReader["name_of_field"].ToString();
switch (sType)
{
case "Low": txtDesc.MaxLength = 5; break;
case "Medium": txtDesc.MaxLength = 10; break;
case "High": txtDesc.MaxLength = 1; break;
}
}
此外,您还需要告诉读者要读回的字段的名称(或索引)
让我指出代码中的一个大问题。它是在方法开始时进行的字符串连接,用于准备命令文本。永远不要使用字符串连接,而应始终使用参数化查询
string constring = "Data Source=.;Initial Catalog=db.MDF;Integrated Security=True";
string Query = "select * from RePriorities where Priority=@priority";
using(SqlConnection conDataBase = new SqlConnection(constring))
using(SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase))
{
conDataBase.Open();
cmdDataBase.Parameters.AddWithValue("@priority", cbType.SelectedItem.ToString());
......
// the rest of your code
}
编辑我忘记为建议添加解释,以避免字符串连接。这里是一篇来自internet搜索的示例文章,它将解释sql命令中字符串连接出现的问题打开SqlDataReader后,您需要调用Read方法将内部记录指针放置到第一条记录。只有在这之后,您才能从读取器中提取值
myReader = cmdDataBase.ExecuteReader();
if(myReader.Read())
{
string sType = myReader["name_of_field"].ToString();
switch (sType)
{
case "Low": txtDesc.MaxLength = 5; break;
case "Medium": txtDesc.MaxLength = 10; break;
case "High": txtDesc.MaxLength = 1; break;
}
}
此外,您还需要告诉读者要读回的字段的名称(或索引)
让我指出代码中的一个大问题。它是在方法开始时进行的字符串连接,用于准备命令文本。永远不要使用字符串连接,而应始终使用参数化查询
string constring = "Data Source=.;Initial Catalog=db.MDF;Integrated Security=True";
string Query = "select * from RePriorities where Priority=@priority";
using(SqlConnection conDataBase = new SqlConnection(constring))
using(SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase))
{
conDataBase.Open();
cmdDataBase.Parameters.AddWithValue("@priority", cbType.SelectedItem.ToString());
......
// the rest of your code
}
编辑我忘记为建议添加解释,以避免字符串连接。这里是一篇来自internet搜索的示例文章,它将解释sql命令中字符串连接出现的问题,描述“但它不起作用”。不清楚。将sType输出到txtDesc,可能有助于调试代码。开关点上的sType值是多少?您是否意识到
MaxLength
文本长度不是文本框的大小?请描述“但它不工作”。不清楚。将sType输出到txtDesc,可能有助于调试代码。开关点的sType值是多少?您是否意识到MaxLength
文本长度不是文本框的大小?