Ayuda en funcion para conectarme con sql
Publicado por miguel (3 intervenciones) el 18/04/2007 22:45:10
habia hecho una función que me transformaba los datos que obtenia de una base de datos acces . pero ahora tengo que hacer lo mismo pero con una base de datos sql server.
si alguien me pudiera ayudar
Funcion que me transformava en xml desde acces , la modifique tratando de que funcionara con sql .
'Connect to the data source.
'Dim objConn As New System.Data.OleDb.OleDbDataAdapter(strConn)
Try
sub Page_Load.open()
'Fill the dataset with records from the Customers table.
Dim strSQL As String
Dim objDataset As New DataSet
Dim objAdapter As New System.Data.OleDb.OleDbDataAdapter
'The OLEDbDataAdapter acts as a bridge between the data source,
'in this case the Customers table, and the dataset.
objDataset.GetXmlSchema()
strSQL = "SELECT id, fecha, fuente, autor, pais, titulo, contenido, destacado, acciones FRO" & _
"noticias"
Me.OleDbSelectCommand1.Connection() = Me.OleDbConnection1
objAdapter.SelectCommand = New System.Data.OleDb.OleDbCommand( _
strSQL) 'objConn)
objAdapter.Fill(objDataset)
'Create the FileStream to write with.
Dim strFilename As String
strFilename = "C:\noticias_vf.xml"
Dim fs As New System.IO.FileStream(strFilename, _
System.IO.FileMode.Create)
'Create an XmlTextWriter for the FileStream.
Dim xtw As New System.Xml.XmlTextWriter(fs, _
System.Text.Encoding.Unicode)
'Add the processing instruction to the beginning of the XML
' file, leaving the one which indicates a style sheet commented.
xtw.WriteProcessingInstruction("xml", "version='1.0'")
'xtw.WriteProcessingInstruction( _
'"xml-stylesheet", "type='text/xsl' href='customers.xsl'")
'xtw.
'Write the XML from the dataset to the file.
objDataset.WriteXml(xtw)
objDataset.GetXml()
xtw.Close()
MsgBox("Customer data has been exported to C:\noticias_vf.xml.")
Catch ex As Exception
' MsgBox(ex.Message)
End Try
End Sub
encontre una forma de conectarme con la bd con el siguiente codigo , como podria llamarlo para poder leer la bd Sql.
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
'Se define el objeto conexión
Dim conn As System.Data.SqlClient.SqlConnection
Dim reader As System.Data.SqlClient.SqlDataReader
Dim sql As System.Data.SqlClient.SqlCommand
'Se especifica el string de conexión
conn = New System.Data.SqlClient.SqlConnection
conn.ConnectionString = "data source=SQLVF-01;integrated security=SSPI;initial catalog=SQLOLEDB"
'Se abre la conexión y se ejecuta la consulta
conn.Open()
sql = New System.Data.SqlClient.SqlCommand
sql.CommandText = "SELECT * FROM noticias"
sql.Connection = conn
reader = sql.ExecuteReader()
Do While reader.Read()
Response.Write(reader.GetValue(0) + "<BR>")
Loop
End Sub
Gracias
si alguien me pudiera ayudar
Funcion que me transformava en xml desde acces , la modifique tratando de que funcionara con sql .
'Connect to the data source.
'Dim objConn As New System.Data.OleDb.OleDbDataAdapter(strConn)
Try
sub Page_Load.open()
'Fill the dataset with records from the Customers table.
Dim strSQL As String
Dim objDataset As New DataSet
Dim objAdapter As New System.Data.OleDb.OleDbDataAdapter
'The OLEDbDataAdapter acts as a bridge between the data source,
'in this case the Customers table, and the dataset.
objDataset.GetXmlSchema()
strSQL = "SELECT id, fecha, fuente, autor, pais, titulo, contenido, destacado, acciones FRO" & _
"noticias"
Me.OleDbSelectCommand1.Connection() = Me.OleDbConnection1
objAdapter.SelectCommand = New System.Data.OleDb.OleDbCommand( _
strSQL) 'objConn)
objAdapter.Fill(objDataset)
'Create the FileStream to write with.
Dim strFilename As String
strFilename = "C:\noticias_vf.xml"
Dim fs As New System.IO.FileStream(strFilename, _
System.IO.FileMode.Create)
'Create an XmlTextWriter for the FileStream.
Dim xtw As New System.Xml.XmlTextWriter(fs, _
System.Text.Encoding.Unicode)
'Add the processing instruction to the beginning of the XML
' file, leaving the one which indicates a style sheet commented.
xtw.WriteProcessingInstruction("xml", "version='1.0'")
'xtw.WriteProcessingInstruction( _
'"xml-stylesheet", "type='text/xsl' href='customers.xsl'")
'xtw.
'Write the XML from the dataset to the file.
objDataset.WriteXml(xtw)
objDataset.GetXml()
xtw.Close()
MsgBox("Customer data has been exported to C:\noticias_vf.xml.")
Catch ex As Exception
' MsgBox(ex.Message)
End Try
End Sub
encontre una forma de conectarme con la bd con el siguiente codigo , como podria llamarlo para poder leer la bd Sql.
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
'Se define el objeto conexión
Dim conn As System.Data.SqlClient.SqlConnection
Dim reader As System.Data.SqlClient.SqlDataReader
Dim sql As System.Data.SqlClient.SqlCommand
'Se especifica el string de conexión
conn = New System.Data.SqlClient.SqlConnection
conn.ConnectionString = "data source=SQLVF-01;integrated security=SSPI;initial catalog=SQLOLEDB"
'Se abre la conexión y se ejecuta la consulta
conn.Open()
sql = New System.Data.SqlClient.SqlCommand
sql.CommandText = "SELECT * FROM noticias"
sql.Connection = conn
reader = sql.ExecuteReader()
Do While reader.Read()
Response.Write(reader.GetValue(0) + "<BR>")
Loop
End Sub
Gracias
Valora esta pregunta


0