SystemTextStringBuilder sb = new SystemTextStringBuilder();
SystemDataSqlClientSqlConnection conn = new SystemDataSqlClientSqlConnection("database=数据库名称;server=服务器地址;uid=用户名;pwd=密码;");
connOpen();//打开数据连接
SystemDataSqlClientSqlCommand cmd = new SystemDataSqlClientSqlCommand("select 列名 from 表名 where 表达式", conn);
using (SystemDataSqlClientSqlDataReader reader = cmdExecuteReader())
{
sbAppendFormat("{0}-{1}", reader[0], reader[1]);//这里几列,写几列
}
connDispose();//释放数据库连接资源(可以使用using字段,继承了IDispose接口类都可以使用using释放非托管资源)
SystemIOFileWriteAllText("文件路径",sbToString() );//写入文件
来信已收到。
这是我修改后的代码,请复制运行即可:
Option Explicit
Dim myCon As New ADODBConnection
Dim myRs As New ADODBRecordset
Private Sub Command1_Click()
Dim XueHao, XingMing As String
XueHao = Text1Text
XingMing = Text2Text
If Trim(XueHao) = "" Or Trim(XingMing) = "" Then
MsgBox "学生档案资料不能为空,请填写!"
Exit Sub
End If
myConOpen "Provider=MicrosoftJetOLEDB40;Data Source=db1mdb;"
myRsOpen "select from 学生档案 where 学号='" & Trim(XueHao) & "' and 姓名='" & Trim(XingMing) & "'", myCon
If myRsEOF = False Then
MsgBox "该学生档案已经存在,请重新输入!"
myRsClose
myConClose
Text1SetFocus
Exit Sub
End If
myRsClose
myRsOpen "学生档案", myCon, 3, 2
myRsAddNew
myRsFields("学号") = Text1Text
myRsFields("姓名") = Text2Text
myRsUpdate
myRsClose
myConClose
MsgBox "添加成功!"
Adodc1RecordSource = "select from 学生档案 order by 姓名 "
'DataGrid1DataSource = Adodc1Recordset
MeAdodc1Refresh
MeDataGrid1Refresh
Text1Text = ""
Text2Text = ""
End Sub
Private Sub Command2_Click()
Dim i As Integer, j As Integer
Dim xlApp
Dim xlBook
Dim xlSheet
Set xlApp = CreateObject("ExcelApplication")
xlAppDisplayAlerts = False
Set xlBook = xlAppWorkbooksOpen(AppPath & "\123xls") '打开模板文件,请修改
xlAppVisible = False
Set xlSheet = xlBookWorksheets("工作表1") '设置活动工作表,请修改
With Adodc1
DataGrid1Visible = False
DataGrid1Tag = "busy"
RecordsetMoveFirst
For j = 0 To RecordsetRecordCount - 1
xlSheetCells(j + 4, 4) = RecordsetFields(0)
xlSheetCells(j + 4, 2) = RecordsetFields(1)
DoEvents
RecordsetMoveNext
Next
xlBookSaveAs FileName:=AppPath & "\1xls" '保存模板文件,请修改
xlBookClose (True)
xlAppQuit
Set xlApp = Nothing
End With
DataGrid1Tag = ""
DataGrid1Visible = True
MsgBox "数据文件已成功写入EXCEL模板文件中!"
End Sub
Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
If DataGrid1Tag = "busy" Then Exit Sub
MeAdodc1RecordSource = "select from 学生档案 where 学号 = '" & Trim(DataGrid1Row) & "'"
Text1Text = MeAdodc1RecordsetFields("学号")
Text2Text = MeAdodc1RecordsetFields("姓名")
End Sub
Private Sub Form_Load()
'Text3Text = DataGrid1Bookmark '获取当前指定的行号
'MeAdodc1ConnectionString = "Provider=MicrosoftJetOLEDB40;Data Source=d:\db1mdb;Persist Security Info=False"
'MeAdodc1RecordSource = "select from 学生档案"
End Sub
以上就是关于.net将数据库查询到的数据写入TXT文本全部的内容,包括:.net将数据库查询到的数据写入TXT文本、求助,怎样把数据库查询出来的数据写入EXCEL里、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)