重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
我是用C#.net写出来的
创新互联成都网站建设按需制作网站,是成都网站设计公司,为混凝土搅拌站提供网站建设服务,有成熟的网站定制合作流程,提供网站定制设计服务:原型图制作、网站创意设计、前端HTML5制作、后台程序开发等。成都网站营销推广热线:028-86922220
SqlConnection myConn = new SqlConnection(sqlconnstring);
myConn.Open();
SqlDataAdapter thisAdapter = new SqlDataAdapter("SELECT * from 表名", myConn);
thisAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
DataTable thisDataTable = new DataTable();
thisAdapter.Fill(thisDataTable);
DataTableReader thisReader = new DataTableReader(thisDataTable);
DataTable schemaTable = thisReader.GetSchemaTable();
foreach (DataRow dr in schemaTable.Rows)
{
MessageBox.Show(dr["ColumnName"].ToString());获取字段名称(f1 f2 f3)
MessageBox.Show(dr["ColumnSize"].ToString());获取字段长度(2 2 2)
MessageBox.Show(dr["DataType"].ToString());获取字段类(str str int)
}
下面是逐个显示第一条记录的各个字段值,通过实例,你应该可以操作想要的信息数据了:
'如果表里的记录数大于0
If ds1.Tables("kucun").Rows.Count 0 Then
'依照数据表的字段值循环
For i = 0 To ds1.Tables("kucun").Columns.Count - 1
'显示第一条记录的第i个字段名和字段值
MsgBox(ds1.Tables("kucun").Columns(i).ColumnName ":" ds1.Tables("kucun").Rows(0)(i))
Next
End If
Option Explicit On
Option Strict On
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class Program
Public Shared Sub Main()
Dim connectionString As String = _
"Data Source=(local);Initial Catalog=Northwind;" _
"Integrated Security=true"
' Provide the query string with a parameter placeholder.
Dim queryString As String = _
"SELECT ProductID, UnitPrice, ProductName from dbo.Products " _
"WHERE UnitPrice @pricePoint " _
"ORDER BY UnitPrice DESC;"
' Specify the parameter value.
Dim paramValue As Integer = 5
' Create and open the connection in a using block. This
' ensures that all resources will be closed and disposed
' when the code exits.
Using connection As New SqlConnection(connectionString)
' Create the Command and Parameter objects.
Dim command As New SqlCommand(queryString, connection)
command.Parameters.AddWithValue("@pricePoint", paramValue)
' Open the connection in a try/catch block.
' Create and execute the DataReader, writing the result
' set to the console window.
Try
connection.Open()
Dim dataReader As SqlDataReader = _
command.ExecuteReader()
Do While dataReader.Read()
Console.WriteLine( _
vbTab "{0}" vbTab "{1}" vbTab "{2}", _
dataReader(0), dataReader(1), dataReader(2))
Loop
dataReader.Close()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Console.ReadLine()
End Using
End Sub
End Class
这是我在vs2010中微软自带的MSDN示例代码里面拷的,是关于ADO.net连接sql的操作。
希望对你有帮助。 如果你还需要其他的,我也可以再拷给你看。