C#项目学习技术总结 (1)

1)窗体隐藏

     this.Hide();Form2 form2 = new Form2();

     form2.ShowDialog();会实现隐藏主窗口功能。

     form2.Show();则不会 。

2S Q L图片的存储

Byte[] pic = null;

    MemoryStream ms = new MemoryStream();

    pictureBox1.Image.Save(ms, ImageFormat.Jpeg);//把图片存储到流中

    pic = new Byte[ms.Length];

    ms.Position = 0;//把流的起点位置设为0

    ms.Read(pic, 0, Convert.ToInt32(ms.Length));

String sqlstr = "update StudentName set pic=@pictuer where nostudent=@id";

    SqlConnection con = Class1.conn;//创建SqlConnection

    SqlCommand sqlcom = new SqlCommand(sqlstr, con);//创建SqlCommand

    con.Open();//打开SQL连接

    sqlcom.Parameters.Add("@pictuer", SqlDbType.Image);//参数的填加

    sqlcom.Parameters.Add("@id", SqlDbType.NChar);

    sqlcom.Parameters["@pictuer"].Value = pic; //参数的赋值

    sqlcom.Parameters["@id"].Value = textBox2.Text;

    sqlcom.ExecuteNonQuery();//执行命令

    con.Close();

3S Q L图片的读取

    SqlConnection conn = Class1.conn;

SqlCommand mycom = new SqlCommand("select * from studentName where    nostudent='" + textBox2.Text + "'", conn);

conn.Open();

    SqlDataReader dr = mycom.ExecuteReader();

dr.Read();

    MemoryStream mstream = new MemoryStream((byte[])dr[6]);//创建图片流对象

pictureBox1.Image = Image.FromStream(ms);

//this.pictureBox1.Image = new Bitmap(mstream);

4ToolBar 控件的运用

       -1- Buttons 集合中添加按钮项。

 

-2-ToolBar添加Images.

添加p_w_picpathList控件,然后为其添加图片。



 

完毕

事件响应处理

private void toolBar1_ButtonClick(object sender, ToolBarButtonClickEventArgs e)

        {

            switch (toolBar1.Buttons.IndexOf(e.Button))

            {

          

                case 3:

                    MessageBox.Show("3");

                    break;

                case 2:

                    MessageBox.Show("2");

                    break;

                case 1:

                    MessageBox.Show("1");

                    break;

                case 0:

                    MessageBox.Show("0");

                    break;

            }

        }

5ContextMenu 控件的运用

       //contextMenu1添加事件处理程序

for (int i=0 ; i< this.contextMenu1.MenuItems.Count ; i++)

       {

           MenuItem mi = contextMenu1.MenuItems[i];

           mi.Click += new EventHandler(menuItem1_Click);

  }

 

//contextMenu2添加项和事件处理程序

 for (int j = 56; j < FontFamily.Families.Length; j++)

        {

           MenuItem mi = new MenuItem();

           mi.Text = FontFamily.Families[j].Name.ToString();

           mi.Click += new EventHandler(menuItem2_Click);

           this.contextMenu2.MenuItems.Add(mi);

        }

 7S Q L文本的保存

       richTextBox1.SaveFile( "temp.rtf" ); //保存到temp.rtf

stream = new FileStream("temp.rtf", FileMode.Open, FileAccess.Read);   //在以文件流的形似读取出来

       int size = Convert.ToInt32(stream.Length);

       Byte[] rtf = new Byte[size];

       stream.Read(rtf, 0, size); //从文件流中读取数据

       try//进行保存

       {

string strSqlCmd = "update examPaper set test=@photo,rightanswer     ='"+ SetRight(this.textBox2.Text.Trim()).ToUpper() +"',type='"+ this.comboBox1.Text +"' where notest="+this.textBox1.Text;

           SqlCommand  cmd = new SqlCommand( strSqlCmd,conn );

           conn.Open();

           cmd.Parameters.Add("@photo",SqlDbType.Image);

           cmd.Parameters["@photo"].Value =rtf;

int intS = cmd.ExecuteNonQuery();

}

.

.

.

.

6S Q L文本的读取

SqlDataReader dr=MyClass.ExecuteRead("select * from ExamPaper where notest="+Paperindex);

       if (dr.Read())

       {

           Byte [] rtf =(byte[])dr["test"];

ASCIIEncoding encoding = new ASCIIEncoding();

                             richTextBox1.Rtf=encoding.GetString(rtf,0,Convert.ToInt32(rtf.Length));

 

8RichTextBox 文本字体的改变

     Font myfont = new Font(myitem.Text,nowFont.Size);

     this.richTextBox1.SelectionFont =myfont;

    

private void ChangeStyle(FontStyle mystyle,Font nowfont)

       {

Font myfont = new Font(nowfont,nowfont.Style | mystyle);

           this.richTextBox1.SelectionFont=myfont;

       }

 

        Font nowFont=this.richTextBox1.SelectionFont;

     if (nowFont == null)

     return;

     switch (e.Button.Tag.ToString())

       {

           case "0":

              ChangeStyle(FontStyle.Bold,nowFont);

              break;

           case "1":

              ChangeStyle(FontStyle.Italic,nowFont);

              break;

           case "2":

              ChangeStyle(FontStyle.Underline,nowFont);

              break;

           case "3":

              ChangeStyle(FontStyle.Strikeout,nowFont);

              break;

           case "4":

this.richTextBox1.SelectionFont=new Font(this.richTextBox1.SelectionFont,FontStyle.Regular);

           break;

}

9S Q L存储过程(事例)

 

set ANSI_NULLS ON

set QUOTED_IDENTIFIER ON

go

 

ALTER PROCEDURE [dbo].[sp_login] (@nostudent varchar(10),@mima varchar(10)) AS

 

declare @kcid varchar(10),

    @classid varchar(10),

    @times int,

    @Usertime int,

    @PaperNum int,

    @mark int,

    @kcname varchar(10),

    @DateType varchar(10),

    @names varchar(10)

 

if exists (select name from studentname where nostudent =@nostudent and mima=@mima)

   begin

       select  @classid=banji,@names=name from studentname where nostudent =@nostudent

       select  @kcid=kcid, @DateType=DateType from tbl_xuanke where classid=@classid and stats='T'

       select @times=times,@PaperNum=PaperNum,@mark=mark,@kcname=kcname from tbl_kc  where  kcid=@kcid

 

       if  @kcid is null

            begin

         select erro =1,errortext='考试还没有开放!!!'

         return                  

            end

 

       if @times is null

            begin

         select erro =1,errortext='试卷没有设置好!!!'

         return                  

            end

 

       if not exists(select * from record where nostudent=@nostudent ) 

           begin

        insert into record values(@nostudent,@kcid,0)

        set @UserTime=0

           end

       else

           begin

        select @Usertime=UserTime from record where nostudent =@nostudent

           if (@times * 60 <=@UserTime)

              begin

           select erro =1,errortext='考试时间已结束!'

            return

              end

           end      

     select error=0,@nostudent,@names,@kcid,@kcname,@times,@PaperNum,@mark,@DateType,@Usertime

     return                

   end

 

  select error=1,errortext='无此考生或密码错误!'

          --if exists(select * from record where nostudent=@nostudent)

 

 

//--执行存储过程查询

string sqlconn="sp_login '"+ this.txtKao.Text.Replace("'","\"") +"','"+this.txtPass.Text.Replace("'","\"") +"'";

SqlDataAdapter myada= new SqlDataAdapter(sqlconn,conn);