发布网友 发布时间:2022-04-08 18:01
共1个回答
热心网友 时间:2022-04-08 19:31
将DataSet数据集写入数据库:string strConnect = Provider=SQLOLEDB.1;Password=1234;Persist Security Info=True; +User ID=username;Initial Catalog=MyTempDb;Data Source=localhost;OleDbConnection dbConn = new OleDbConnection;dbConn.ConnectionString = strConnect;try{StringBuilder strXml = new StringBuilder();StringWriter strWriter = new StringWriter( strXml );XmlTextWriter writer = new XmlTextWriter(strWriter);writer.WriteStartDocument();m_dsDataSet.WriteXml(writer);string strSQL = INSERT INTO temp(Temp_date,Temp_data) VALUES(GETDATE(),?);OleDbCommand aCommand = new OleDbCommand( strSQL , dbConn );aCommand.Parameters.Add(Temp_data, OleDbType.VarChar).Value = strXml.ToString();aCommand.Connection.Open();aCommand.ExecuteNonQuery();}catch ( OleDbException e ){Debug.WriteLine(发生异常: + e.ToString());}从数据库中读出DataSet数据集:// 这里省略千篇一律的数据库的Query操作OleDbDataReader aReader = aCommand.ExecuteReader( );if (aReader.Read() ){StringBuilder strXml = new StringBuilder( aReader[temp_data].ToString() );StringReader strReader = new StringReader( strXml.ToString() );XmlTextReader reader = new XmlTextReader(strReader);try{m_dsDataSet.ReadXml(reader);aReader.Close( );}catch( OleDbException e ){Debug.WriteLine(发生异常: + e.ToString());}}注意以上例子所引用的变量m_dsDataSet的原始定义为DataSet m_dsDataSet,在这里是一个作者自定义包含有两个表和记录的DataSet实例,其中的记录来源于搜索引擎分析出的每条记录。