您好为此更好的代码..这里是c#的winform代码. [英] hi better code for this..here is winform code for c#.

查看:62
本文介绍了您好为此更好的代码..这里是c#的winform代码.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,这是Windows窗体代码,我想要此代码用于我的网站项目.有谁能帮我吗?我该如何为我的aspx页面提供代码.我想在我的aspx页面中显示与winform中显示的相同的输出

hi here is windows form code i want this code for my website project.can any one help me ??how can i give code for my aspx page.i wanna display same output in my aspx page as displayed in winform

private void button1_Click(object sender, EventArgs e)
      {

          dataGridView1.Rows.Clear();
          dataGridView1.ColumnCount = 10;
          dataGridView1.RowCount = 10;
          dataGridView1.Columns[0].Width = 100;
          dataGridView1.Columns[1].Width = 100;
          dataGridView1.Columns[2].Width = 20;
          dataGridView1.Columns[3].Width = 100;
          dataGridView1.Columns[4].Width = 100;
          dataGridView1.Columns[5].Width = 20;
          dataGridView1.Columns[6].Width = 100;
          dataGridView1.Columns[7].Width = 100;
          //dataGridView1.Rows(0).Cells(0).Value = "Semister";
          dataGridView1.Columns[0].HeaderText = "I Row";
          dataGridView1.Columns[1].HeaderText = "II Row";
          dataGridView1.Columns[2].HeaderText = "";
          dataGridView1.Columns[3].HeaderText = "III Row";
          dataGridView1.Columns[4].HeaderText = "IV Row";
          dataGridView1.Columns[5].HeaderText = "";
          dataGridView1.Columns[6].HeaderText = "V Row";
          dataGridView1.Columns[7].HeaderText = "VI Row";

          int a = 1, b = 0,c=0;
          DateTime d1 = DateTime.Today.Date;
          string str1="";
          str1 = comboBox1.Text;
          d1 = new DateTime(int.Parse(str1.Substring(6, 4)), int.Parse(str1.Substring(3, 2)), int.Parse(str1.Substring(0, 2)));

          if (conn.State == ConnectionState.Open) { conn.Close(); }
          conn.Open();
          SqlCommand cmd = new SqlCommand();
          SqlDataReader dr;
          cmd.Connection = conn;
          cmd.CommandText = "select Regno,deskno,deskside,semister from AllotmentTab where RoomNo=''" + comboBox3.Text + "'' and examdate=''" + d1 + "'' and esession=''" + comboBox2.Text + "''  order by Deskno,deskside";
          dr = cmd.ExecuteReader();
          while (dr.Read())
          {
              b = int.Parse(dr.GetValue(1).ToString());
              if (b > 14)
              { b = b - 14;
              c = 6;
              }
              else if (b > 7)
              { b = b - 7;
              c = 3;
              }
              else
              {
                  //b = b - 7;
                  c = 0;
              }
              if (dr.GetValue(2).ToString() == "L")
              {
                  dataGridView1.Rows[b].Cells[c].Value = dr.GetValue(0).ToString() + " (" + dr.GetValue(3).ToString() + ")";
              }
              else
              {
                  dataGridView1.Rows[b].Cells[c + 1].Value = dr.GetValue(0).ToString() + " (" + dr.GetValue(3).ToString() + ")";
              }
              a++;

              if ((a % 7) == 0)
              {

              }

          }



          //Alloted Faculty :
          if (conn.State == ConnectionState.Open) { conn.Close(); }
          conn.Open();
          SqlCommand cmd5 = new SqlCommand();
          SqlDataReader dr5;
          cmd5.Connection = conn;
          cmd5.CommandText = "select facultycode from fAllotmentTab where RoomNo=''" + comboBox3.Text + "'' and examdate=''" + d1 + "'' and esession=''" + comboBox2.Text + "''";
          dr5 = cmd5.ExecuteReader();
          if (dr5.Read())
          {
              labelmsg.Text = "Alloted Faculty : " + dr5.GetValue(0).ToString();
          }
          else { labelmsg.Text = "Alloted Faculty : "; }


      }

      private void RoomwiseDisForm_Load(object sender, EventArgs e)
      {
          for (int i = 101; i < 111; i++)
          {
              comboBox3.Items.Add(i.ToString());
          }

          if (conn.State == ConnectionState.Open) { conn.Close(); }
          conn.Open();
          SqlCommand cmd = new SqlCommand();
          SqlDataReader dr;
          cmd.Connection = conn;
          cmd.CommandText = "select ExamDate from AllotmentTab group by ExamDate  order by ExamDate";
          dr = cmd.ExecuteReader();
          while (dr.Read())
          {
              comboBox1.Items.Add(string.Format("{0:dd-MM-yyyy}",dr.GetValue(0)));

          }

      }



这是设计代码.



and this is design code..

private void InitializeComponent()
       {
           this.button1 = new System.Windows.Forms.Button();
           this.labelmsg = new System.Windows.Forms.Label();
           this.comboBox3 = new System.Windows.Forms.ComboBox();
           this.label1 = new System.Windows.Forms.Label();
           this.dataGridView1 = new System.Windows.Forms.DataGridView();
           this.comboBox1 = new System.Windows.Forms.ComboBox();
           this.label2 = new System.Windows.Forms.Label();
           this.comboBox2 = new System.Windows.Forms.ComboBox();
           this.label3 = new System.Windows.Forms.Label();
           this.button2 = new System.Windows.Forms.Button();
           ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
           this.SuspendLayout();
           //
           // button1
           //
           this.button1.Location = new System.Drawing.Point(475, 28);
           this.button1.Name = "button1";
           this.button1.Size = new System.Drawing.Size(133, 31);
           this.button1.TabIndex = 22;
           this.button1.Text = "Display";
           this.button1.UseVisualStyleBackColor = true;
           this.button1.Click += new System.EventHandler(this.button1_Click);
           //
           // labelmsg
           //
           this.labelmsg.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(224)))), ((int)(((byte)(192)))));
           this.labelmsg.Location = new System.Drawing.Point(24, 89);
           this.labelmsg.Name = "labelmsg";
           this.labelmsg.Size = new System.Drawing.Size(431, 16);
           this.labelmsg.TabIndex = 21;
           this.labelmsg.Text = "Alloted Faculty :";
           //
           // comboBox3
           //
           this.comboBox3.FormattingEnabled = true;
           this.comboBox3.Location = new System.Drawing.Point(27, 35);
           this.comboBox3.Name = "comboBox3";
           this.comboBox3.Size = new System.Drawing.Size(93, 24);
           this.comboBox3.TabIndex = 20;
           //
           // label1
           //
           this.label1.AutoSize = true;
           this.label1.Location = new System.Drawing.Point(24, 16);
           this.label1.Name = "label1";
           this.label1.Size = new System.Drawing.Size(65, 16);
           this.label1.TabIndex = 19;
           this.label1.Text = "Semister";
           //
           // dataGridView1
           //
           this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
           this.dataGridView1.Location = new System.Drawing.Point(27, 109);
           this.dataGridView1.Margin = new System.Windows.Forms.Padding(4);
           this.dataGridView1.Name = "dataGridView1";
           this.dataGridView1.Size = new System.Drawing.Size(971, 485);
           this.dataGridView1.TabIndex = 18;
           //
           // comboBox1
           //
           this.comboBox1.FormattingEnabled = true;
           this.comboBox1.Location = new System.Drawing.Point(172, 35);
           this.comboBox1.Name = "comboBox1";
           this.comboBox1.Size = new System.Drawing.Size(165, 24);
           this.comboBox1.TabIndex = 24;
           //
           // label2
           //
           this.label2.AutoSize = true;
           this.label2.Location = new System.Drawing.Point(169, 16);
           this.label2.Name = "label2";
           this.label2.Size = new System.Drawing.Size(39, 16);
           this.label2.TabIndex = 23;
           this.label2.Text = "Date";
           //
           // comboBox2
           //
           this.comboBox2.FormattingEnabled = true;
           this.comboBox2.Items.AddRange(new object[] {
           "M",
           "A"});
           this.comboBox2.Location = new System.Drawing.Point(362, 35);
           this.comboBox2.Name = "comboBox2";
           this.comboBox2.Size = new System.Drawing.Size(93, 24);
           this.comboBox2.TabIndex = 26;
           this.comboBox2.Text = "M";
           //
           // label3
           //
           this.label3.AutoSize = true;
           this.label3.Location = new System.Drawing.Point(359, 16);
           this.label3.Name = "label3";
           this.label3.Size = new System.Drawing.Size(58, 16);
           this.label3.TabIndex = 25;
           this.label3.Text = "Session";
           //
           // button2
           //
           this.button2.Location = new System.Drawing.Point(865, 35);
           this.button2.Name = "button2";
           this.button2.Size = new System.Drawing.Size(133, 31);
           this.button2.TabIndex = 27;
           this.button2.Text = "Close";
           this.button2.UseVisualStyleBackColor = true;
           this.button2.Click += new System.EventHandler(this.button2_Click);
           //
           // RoomwiseDisForm
           //
           this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
           this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
           this.ClientSize = new System.Drawing.Size(1028, 607);
           this.Controls.Add(this.button2);
           this.Controls.Add(this.comboBox2);
           this.Controls.Add(this.label3);
           this.Controls.Add(this.comboBox1);
           this.Controls.Add(this.label2);
           this.Controls.Add(this.button1);
           this.Controls.Add(this.labelmsg);
           this.Controls.Add(this.comboBox3);
           this.Controls.Add(this.label1);
           this.Controls.Add(this.dataGridView1);
           this.Font = new System.Drawing.Font("Verdana", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
           this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
           this.Name = "RoomwiseDisForm";
           this.Text = "RoomwiseDisForm";
           this.Load += new System.EventHandler(this.RoomwiseDisForm_Load);
           ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
           this.ResumeLayout(false);
           this.PerformLayout();

       }





任何人都可以帮助我..





can any one help me..

推荐答案

您需要做的第一件事是取好表格并重新设计该网页-我知道这很明显,但是这确实是连接UI的起点.尽管您可以使控件名称保持相同,但我认为您应该花些时间重命名它们,因为您在此处使用默认值,这并不是您能做的最好的事情.重命名它们的原因是为了让您以后可以通过查看名称来知道它们的含义,而不必在表单上四处寻找哪个控件恰好与comboBox5相匹配.

您的代码面临的最大问题是,它们全部以一种形式发生.这里根本没有分层设计.对于大多数Web应用程序,您希望将功能划分为多个层",以便可以轻松隔离和测试代码.如果要解决这个问题,我希望将数据库代码移到一个项目中,将业务逻辑移到另一个项目中,然后将UI映射到这些项目上.不过,您要考虑的一个关键点是,由于Web应用程序已与服务器断开连接,因此您应该寻求尽快获取与数据库的连接并尽快释放它.另外,您应该真正使用数据库参数,而不是使用字符串连接-如果您不这样做,则会使自己对SQL注入攻击敞开大门.
The first thing you need to do is take your form and redesign it for the web page - I know this seems obvious, but it''s really the starting point for hooking the UI up. While you could keep the control names the same, I think you should take the time to rename them as you are using the default values here, which is not the best thing you can do. The reason for renaming them is so that you can tell later on, what they are meant to do by looking at the name, rather than having to hunt around on the form seeing which control happens to match comboBox5.

The biggest issue you face with your code is that it all happens on a single form. There''s no tier design here at all. With most web applications you would look to split the functionality over multiple "tiers" so that you can isolate and test your code with ease. If I were tackling this, I would look to move the database code into one project, business logic into another and then have the UI just map onto these. A key point for you to consider though is that, as your web application is disconnected from the server, you should be looking to acquire the connection to the database as late as possible and release it as soon as possible. Also, you should really look to use database parameters, rather than using string concatenation - if you don''t, you''re leaving yourself wide open to SQL Injection Attacks.


您好,我没有为您的项目提供代码,因为您必须自己完成代码.
但是
我提出了一些改进您的代码的建议:

1.不要使用重复代码("dataGridView1.Columns[0].Width = 100"),请使用"for(int i =0;i<8;i++)".

2.使用Array:
Hello, I don''t give you code for your project because you have to do it by yourself.
But
I make a few suggestion to improve your code:

1. Don''t use repetitious code ("dataGridView1.Columns[0].Width = 100") use "for(int i =0;i<8;i++)" for it.

2. Use Array :
string headers = new string[8]{"I Row","II Row",...};

//in a for loop:
{
    dataGridView1.Columns[i].HeaderText = headers[i];
}



3.注入风险:(安全警告)
不要使用cmd.CommandText ="Blah Blah Blah" +一些控制值+ ....
使用command.Parameters

4.最好使用Switch/Case而不是if/else if/else if/else if/...

5.尝试使用一行如果"代替if/else,例如:



3. Injection Risk: (Security warning)
Do not use cmd.CommandText = "Blah Blah Blah " + some control value + ....
use command.Parameters

4. It''s better to use Switch/Case instead of using if/else if/else if/else if/...

5. Try to use "one Line if" instead of if/else, for example:

//if (dr5.Read())
//{
//    labelmsg.Text = "Alloted Faculty : " + dr5.GetValue(0).ToString();
//}
//else { labelmsg.Text = "Alloted Faculty : "; }

labelmsg.Text = (dr5.Read())? "Alloted Faculty : " + dr5.GetValue(0).ToString(): "Alloted Faculty : ";


6. SQL操作后关闭连接.试试看/抓住/最后
例如:


6. Close the connection after SQL operation. Have a try/catch/finally
for example:

try
{
   SqlCommand cmd = new SqlCommand();
            SqlDataReader dr;
  //...
}
catch(Exception ex)
{
  //...
}
finally
{
   conn.Close();
}



7.为什么不使用ASP.Net Content而不是程序逻辑来生成控件?



7. Why don''t you use ASP.Net Content instead of program logic to generate the controls?


这篇关于您好为此更好的代码..这里是c#的winform代码.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆