c# winform学习记录(链接数据库,修改字体颜色)

时间:2022-11-27 09:43:38
  • sql server数据库配置(windows 身份认证)
    •   config文件添加如下代码
      •   
        <connectionStrings>
        <add name="conn" connectionString="Server=**;Integrated Security=SSPI;" />
        </connectionStrings>
        代码添加
        • //开头添加
          using System.Configuration;
          using System.Data.SqlClient;

          //链接
          string connstr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
          SqlConnection conn
          = new SqlConnection(connstr);
          conn.Open();

          查询数据

          SqlCommand com = new SqlCommand("select * from table", conn);
          SqlDataAdapter sda
          = new SqlDataAdapter(com);
          sda.Fill(dt);
          if (dt.Rows.Count > 0)
          {
            
          for (int j = 0; j < result.Rows.Count; j++)
              {
                
          for (int i = 0; i < result.Columns.Count; i++)
                  {
            
          this.richTextBox1.AppendText(dt.Rows[j][i].ToString() + " ");
          }
          this.richTextBox1.AppendText("\n");
          }
          }
             
      •   修改,插入,更新
        SqlCommand command = new SqlCommand(sql, conn);
        command.ExecuteNonQuery();

         

  • 修改富文本框关键字颜色和字体
    •  修改富文本框中特定字符的颜色和字体,支持重复出现情况
      private void modifyFont(string p)
      {
      string s = richTextBox1.Text;
      int M = p.Length; int N = s.Length;
      char[] ss = s.ToCharArray(), pp = p.ToCharArray();
      for (int i = 0; i < N - M + 1; i++)
      {
      int j;
      for (j = 0; j < M; j++)
      {
      if (ss[i + j] != pp[j]) break;
      }
      if (j == p.Length)
      {
      richTextBox1.Select(i, p.Length);
      richTextBox1.SelectionColor
      = Color.Red;
      richTextBox1.SelectionFont
      = new Font(Font, FontStyle.Bold);
      }
      }
      }