问题描述
服务端usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Runtime;usingSystem.Runtime.Remoting;usingSystem.Runtime.Remoting.Channels;usingSystem.Runtime.Remoting.Channels.Tcp;usingDataBase;namespaceRemotingServer{publicpartialclassServer:Form{publicServer(){InitializeComponent();}privatevoidstart_Click(objectsender,EventArgse){MessageBox.Show("服务已启动");}privatevoidclose_Click(objectsender,EventArgse){this.Close();}privatevoidServer_Load(objectsender,EventArgse){//用构造函数初始化监听端口,两种方式,这里用TCP//HttpServerChannelchannel2=newHttpServerChannel(8020);TcpServerChannelchannel=newTcpServerChannel(8086);ChannelServices.RegisterChannel(channel,false);//向信道服务注册信道StudentConnectstudent=newStudentConnect();RemotingConfiguration.RegisterWellKnownServiceType(typeof(StudentConnect),"Student",WellKnownObjectMode.SingleCall);}}}客户端usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingService;usingServiceImp;usingModel;usingSystem.Runtime;usingSystem.Runtime.Remoting;usingSystem.Runtime.Remoting.Channels;usingSystem.Runtime.Remoting.Channels.Tcp;usingDataBase;namespaceStudentSystem{publicpartialclassMainJFrame:Form{StudentConnectstudent=(StudentConnect)Activator.GetObject(typeof(StudentConnect),"tcp://localhost:8086/Student");publicMainJFrame(){InitializeComponent();}privatevoidadd_Click(objectsender,EventArgse){AddStudentadd=newAddStudent();this.Visible=false;add.ShowDialog();}privatevoidbutton1_Click(objectsender,EventArgse){//创建对象实例,确定位置、大小ListViewlstView=newListView();lstView.Location=newPoint(50,50);lstView.Size=newSize(300,300);//创建列标题ColumnHeaderheader1=newColumnHeader();header1.Text="编号";ColumnHeaderheader2=newColumnHeader();header2.Text="姓名";ColumnHeaderheader3=newColumnHeader();header3.Text="性别";ColumnHeaderheader4=newColumnHeader();header4.Text="密码";//将列标题添中到listview中lstView.Columns.AddRange(newColumnHeader[]{header1,header2,header3,header4});//关联listview的imagelist//lstView.SmallImageList=this.imageList1;try{List<Student>list=student.SelectStudent();foreach(Studentstuinlist){ListViewItemitem=newListViewItem(stu.StudentId.ToString(),list.Count);item.SubItems.Add(stu.Name);item.SubItems.Add(stu.Sex);item.SubItems.Add(stu.Sex);item.SubItems.Add(stu.Password);//将行添加到listview中lstView.Items.AddRange(newListViewItem[]{item});}//设置listview的显示视图为详细视图lstView.View=View.Details;//设置点击选中一行lstView.FullRowSelect=true;//将生成的控件添加到窗体this.Controls.Add(lstView);}catch(Exception){MessageBox.Show("网络掉线,请重新打开软件!");}}privatevoidbutton2_Click(objectsender,EventArgse){StudentServiceservice=newStudentServiceImp();DataSetds=service.FindStudent();this.studentFind.DataSource=ds.Tables[0];service.Close();}}}数据库操作usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Data.SqlClient;usingSystem.Data;usingModel;usingSystem.Configuration;namespaceDataBase{[Serializable]publicclassStudentConnect:MarshalByRefObject{privatestaticstringstrConn=ConfigurationManager.ConnectionStrings["SQLConnString"].ConnectionString;privateSqlConnectioncon=newSqlConnection();privateSqlDataAdapterda=newSqlDataAdapter();privateDataSetds=newDataSet();privateSqlCommandcom=null;privateSqlDataReaderdr=null;///<summary>///学生信息查询操作///</summary>///<returns></returns>publicDataSetFindStudent(){con=DBConnect.GetConnection();stringsql="selects.student_idas编号,s.nameas姓名,s.sexas性别,s.passwordas密码,c.class_nameas班级名称fromstudentsleftjoinclasscons.class_id=c.class_id";com=newSqlCommand(sql,con);da.SelectCommand=com;da.Fill(ds);returnds;}publicList<Student>SelectStudent(){con=DBConnect.GetConnection();stringsql="selectstudent_id,name,sex,passwordfromstudent";com=newSqlCommand(sql,con);dr=com.ExecuteReader();List<Student>list=newList<Student>();while(dr.Read()){Studentstudent=newStudent();student.StudentId=(int)dr["student_id"];student.Name=dr["name"].ToString();student.Sex=dr["sex"].ToString();student.Password=dr["password"].ToString();list.Add(student);continue;}returnlist;}publicstaticDataSetGetDataSet(stringstrSQL,SqlParameter[]pas,CommandTypecmdtype){DataSetdt=newDataSet();using(SqlConnectionconn=newSqlConnection(strConn)){SqlDataAdapterda=newSqlDataAdapter(strSQL,conn);da.SelectCommand.CommandType=cmdtype;if(pas!=null){da.SelectCommand.Parameters.AddRange(pas);}da.Fill(dt);}returndt;}publicvoidInsertStudent(Studentstudent){con=DBConnect.GetConnection();da=newSqlDataAdapter();Stringsql="insertintostudent(name,sex,password,class_id)values(@name,@sex,@password,@class_id)";com=newSqlCommand(sql,con);//com.CommandText="";com.Parameters.AddWithValue("@name",student.Name);com.Parameters.AddWithValue("@sex",student.Sex);com.Parameters.AddWithValue("@password",student.Password);com.Parameters.AddWithValue("@class_id",student.ClassId);da.InsertCommand=com;da.InsertCommand.ExecuteNonQuery();}///<summary>///关闭连接///</summary>publicvoidClose(){if(dr!=null){dr.Close();}if(con!=null){con.Close();}}//endClose}}启动之后怎么就实现不了效果呢
解决方案
解决方案二:
代码好长,运行有错误吗?如果有的话,贴下异常信息。
解决方案三:
先自己调试,找到问题的代码,只贴问题代码