public static void insertFileToDB(Socket socket) { byte[] b = new byte[socket.SendBufferSize]; int k = socket.Receive(b); // get myFile object in bytes Console.WriteLine("Recieved..."); string msg = ""; for (int i = 0; i < k; i++) // convert to string { msg += Convert.ToChar(b[i]); } Array.Clear(b, 0, b.Length); Console.Write(msg); MyFile file = new MyFile(); Object obj = DeSerializeAnObject(msg, file.GetType()); // convert from string to myFile object (2 rows) MyFile newFile = (MyFile)obj; int ans = data.isFileExist(newFile.fileName, newFile.IP); // checks if the file exist in system string str = ""; if (ans < 1) // no rows found { str = "Not Exists"; data.insertFile(newFile.fileName, newFile.size, newFile.IP, newFile.port, newFile.path); } else { str = "Exists"; } ASCIIEncoding asen = new ASCIIEncoding(); byte[] flag = asen.GetBytes(str); // return answer from server to client socket.Send(flag); }
//在datagrid中绑定数据库数据 public void BindData() { using (OleDbConnection connection = DbHelper.getCon()) { connection.Open(); OleDbDataAdapter adapter = new OleDbDataAdapter(); DataSet dataTable = new DataSet(); String selectString = "select * from config1"; OleDbCommand command = new OleDbCommand(selectString, connection); adapter.SelectCommand = command; adapter.Fill(dataTable); var dt = dataTable.Tables[0]; dataTable.Tables[0].PrimaryKey = new DataColumn[] { dataTable.Tables[0].Columns[0] }; dg.ItemsSource = dataTable.Tables[0].DefaultView; connection.Close(); DataRow dr = dataTable.Tables[0].NewRow(); for (int i = 0; i < dt.Rows.Count; i++) { //创建泛型对象 MyFile _t = new MyFile(); _t = Activator.CreateInstance <MyFile>(); //获取对象所有属性 PropertyInfo[] propertyInfo = _t.GetType().GetProperties(); for (int j = 0; j < dt.Columns.Count; j++) { foreach (PropertyInfo info in propertyInfo) { //属性名称和列名相同时赋值 if (dt.Columns[j].ColumnName.ToUpper().Equals(info.Name.ToUpper())) { if (dt.Rows[i][j] != DBNull.Value) { info.SetValue(_t, dt.Rows[i][j], null); } else { info.SetValue(_t, null, null); } break; } } id = i + 1; } files.Add(_t); } } }