首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

关于。NET窗体应用程序开发,不知道为啥不能运行

2012-11-14 
关于。NET窗体应用程序开发,不知道为什么不能运行C# codenamespace DeleteRecord_10_15{public partial cla

关于。NET窗体应用程序开发,不知道为什么不能运行

C# code
namespace DeleteRecord_10_15{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        public static  string constr = "Data Source=*******;Initial Catalog=language;Intergrated Security=true";        public static  SqlConnection con = new SqlConnection(constr);        private void button1_Click(object sender, EventArgs e)        {            int wordsID = 0;            int pharseID = 0;            OpenFileDialog ofd = new OpenFileDialog();            if (ofd.ShowDialog() == DialogResult.OK)            {                FileStream myfile = new FileStream(ofd.FileName,FileMode.Open);                StreamReader myReader = new StreamReader(myfile);                string line = myReader.ReadLine();                if ( line.Trim ()!="" &&line.Trim().Contains(" "))//line不为空 qie 为词组                {                    pharseID = GetPharseID(line);                    if (deleteFromPharseClassification(pharseID))                    {                        if (deleteFromPharse(pharseID))                        {                            MessageBox.Show("删除成功!");                        }                    }                }                else if (line.Trim() != "" && !line.Trim().Contains(" "))//line不为空 qie 为单词                {                    wordsID = this.GetWordsID(line.Trim());                    if (deleteFromWordsclassification(wordsID))                    {                        if (deleteFromWords(wordsID))                        {                            MessageBox.Show("删除成功");                        }                    }                }            }        }        public int GetWordsID(string strWord)        {            string sqlcmd2 = "select wordsID from words where word='" + strWord + "'";            SqlCommand cmd2 = new SqlCommand(sqlcmd2, con);            con.Open();            SqlDataReader reader = cmd2.ExecuteReader();            int ID = Convert.ToInt32(reader[0].ToString());            reader.Close();            con.Close();            return ID;        }        public int GetPharseID(string strPharse)        {            string sqlcmd2 = "select pharseID from pharse where pharse='" + strPharse + "'";            SqlCommand cmd2 = new SqlCommand(sqlcmd2, con);            con.Open();            SqlDataReader reader = cmd2.ExecuteReader();            int ID = Convert.ToInt32(reader[0].ToString());            reader.Close();            con.Close();            return ID;        }        public Boolean deleteFromWordsclassification(int wordsID)        {            Boolean flag = false;            string sqlcmd1 = "delete from wordsclassification where wordsID=" + wordsID;            SqlCommand cmd1 = new SqlCommand(sqlcmd1, con);            int num = 0;            try            {                con.Open();                num = cmd1.ExecuteNonQuery();                con.Close();            }            catch (Exception ex)            {                MessageBox.Show(ex.ToString());            }            finally            {                if (num == 1)                    flag = true;                else                    flag = false;            }            return flag;        }        public Boolean deleteFromWords(int wordsID)        {            Boolean flag = false;            string sqlcmd1 = "delete from words where wordsID =" + wordsID;            SqlCommand cmd1 = new SqlCommand(sqlcmd1, con);            int num = 0;            try            {                con.Open();                num = cmd1.ExecuteNonQuery();                con.Close();            }            catch (Exception ex)            {                MessageBox.Show(ex.ToString());            }            finally            {                if (num == 1)                    flag = true;                else                    flag = false;            }            return flag;        }        public Boolean deleteFromPharseClassification(int pharseID)        {            Boolean flag = false;            string sqlcmd1 = "delete from pharseClassification where pharseID =" + pharseID;            SqlCommand cmd1 = new SqlCommand(sqlcmd1, con);            int num = 0;            try            {                con.Open();                num = cmd1.ExecuteNonQuery();                con.Close();            }            catch (Exception ex)            {                MessageBox.Show(ex.ToString());            }            finally            {                if (num == 1)                    flag = true;                else                    flag = false;            }            return flag;        }        public Boolean deleteFromPharse(int pharseID)        {            Boolean flag = false;            string sqlcmd1 = "delete from pharse where pharseID =" + pharseID;            SqlCommand cmd1 = new SqlCommand(sqlcmd1, con);            int num = 0;            try            {                con.Open();                num = cmd1.ExecuteNonQuery();                con.Close();            }            catch (Exception ex)            {                MessageBox.Show(ex.ToString());            }            finally            {                if (num == 1)                    flag = true;                else                    flag = false;            }            return flag;        }    }} 



语法什么的都没有问题,但就是不能生成窗体,这是为什么???

[解决办法]
你的代码都和窗体是否显示无关。

看看窗口属性是不是设置了隐藏。

或者贴出.Designer.cs中关于Form的代码。

下面是我的测试工程的Form1的代码。

// 
// Form1
// 
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseClick);
this.ResumeLayout(false);
[解决办法]
在普通类中标记静态成员这个类不能标记static因为静态类里面基本都是静态方法,如果这个类需要被实例化并且这个类中有一个成员是所有对象共享的数据时那么这个类中的成员标记为static

静态方法的调用只能通过类名.方法名(Person.show())不能通过对象来调用(错误显示:p.show()这里P是Person的实例化对象)

静态成员是在整个程序退出时才释放资源,因此可以在整个程序中共享数据

应注意的问题:

1,静态类不能被实例化和继承,由于静态成员是在程序退出时才释放资源因此尽量避免写静态字段或静态属性最好只写静态方法

2,当给一个普通类写静态字段后系统会默认生成一个静态构造函数而静态类的构造函数只会在第一次使用静态类之前执行,并且只会执行一次

静态构造函数不能有访问修饰符和参数

静态类不能实现接口因为接口中的成员都是实例成员
[解决办法]
探讨

还是不怎么理解,能举个例子吗?上面只是讲了一下使用Static时的注意事项。但是为什么不能在类中定义一个静态的sqlconnection对象呢?

热点排行