数据的查询:private void FullTab() { SqlConnection con1 = new SqlConnection();//创建数据库库链接 try { String con = "server=.;database=common_wjdl;uid=sa;pwd=db2008mima";//定义数据库连接信息 con1.ConnectionString = con; con1.Open();//打开数据库 String sql = "select * from Fileuploaded";//创建语句查询所有数据 SqlDataAdapter ada1 = new SqlDataAdapter(sql, con);//SqlDataAdapter 对象。 用于填充DataSet (数据集)。 DataSet myset = new DataSet(); SqlCommand cmd = con1.CreateCommand();//创建数据库命令 cmd.CommandText = sql; SqlDataReader reader = cmd.ExecuteReader(); //从数据库中读取数据流存入reader中 } catch (SqlException ee1) { Console.WriteLine(ee1.Message);//打印异常信息 } catch (IndexOutOfRangeException ee2) { Console.WriteLine(ee2.Message);//打印异常信息 } } 数据的插入:(大致都一样,只需修改SQL语句就行) String con = "server=.;database=common_wjdl;uid=sa;pwd=db2008mima";//定义数据库连接信息 con1.ConnectionString = con; con1.Open();//打开数据库 String sql = "insert into Fileuploaded (FileID,FileName,FilePath,QRCodePath,FileSuffix) Values('"+fileNameSave+"','"+fileName+"','"+filePath+"','"+filePath+"','"+fileSuffix+"')";//创建语句向数据库插入数据 SqlCommand cmd = con1.CreateCommand();//创建数据库命令 cmd.CommandText = sql; cmd.ExecuteNonQuery();