首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何创建将字母分配给特定单词的密码?

如何创建将字母分配给特定单词的密码?
EN

Stack Overflow用户
提问于 2020-08-08 12:45:59
回答 1查看 47关注 0票数 0

如何在C#中创建简单的密码应用程序,其中我将字母分配给特定的单词,例如t将代表单词take,因此t将是密码,take将是描述,这些密码可以存储在数据库或文本文件中?

我已经尝试过下面的代码,但它不适用于密码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
private void pictureBox5_Click_1(object sender, EventArgs e)
{
    using (SqlConnection con = new SqlConnection("Data Source=DESKTOP-EU4PTNQ;Initial Catalog=Medrive;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"))
    {
       con.Open();
              
       if (bunifuTextBox2.Text != "")
       {
           string DescriptionQueryStr = "Select CName from Ciphers";
    
           using (SqlCommand cmd = new SqlCommand(DescriptionQueryStr, con))
           {
                        
               string[] tbVals = bunifuTextBox2.Text.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
               for (int i = 0; i < tbVals.Length; i++)
               {
                   DescriptionQueryStr = "where Description=@Description" + i.ToString() + "OR";
                   cmd.Parameters.Add("@Description" + i.ToString(), SqlDbType.VarChar).Value = tbVals[i];
               }
               //Strip off the last OR
               DescriptionQueryStr = DescriptionQueryStr.Substring(0, DescriptionQueryStr.Length - 2);
               StringBuilder sb = new StringBuilder();

               using (SqlDataReader da = cmd.ExecuteReader())
               {
                   if (da.Read())
                   {
                       var hasAnotherRecord = true;
                       while (hasAnotherRecord)
                       {
                           sb.AppendLine(da.GetValue(0).ToString());
                           hasAnotherRecord = da.Read();
  
                           if (hasAnotherRecord )
                              sb.Append('-');
                        }
                    }
                }
 
                TAbunifuTextBox10.Text = sb.ToString();
                System.Diagnostics.Debug.Write("Hello via Debug!");
            }                   
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2020-08-08 15:11:41

您没有将查询条件(WHERE ...)添加到发送到服务器的实际查询中。

您的命令结构如下所示

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
SqlCommand cmd = new SqlCommand(DescriptionQueryStr, conn);

但是您再也不会更改cmd.CommandText (保存发送到sql server的查询的属性)了。因此,发送到服务器的查询是Select CName from Ciphers,它将返回所有行。

在您的for循环中,您正在更改DescriptionQueryStr,但是这些更改永远不会应用到cmd.CommandText。此外,您也没有正确处理条件,因为不是将每个新条件附加到DescriptionQueryStr,而是在循环的每次迭代中替换它。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//Concat all conditions in a String builder
StringBuilder conditions = new StringBuilder();
for (int i = 0; i < tbVals.Length; i++) {
    //if there is more than one condition, add an OR inbetween them
    if (i > 0) conditions.Append(" OR ");  
    conditions.Append($"Description = @Description_{i}");
    cmd.Parameters.Add("@Description_{i}", SqlDbType.VarChar).Value = tbVals[i];
}

//If there is at least one parameter, add the conditions to the query
if (tbVals.Length > 0)
    cmd.CommandText += $" WHERE {conditions.ToString()}";
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63315581

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文