从数据库DataBase ,表Exam的q 字段中读出的程序代码(字符格式),写入tc.cpp文件中?
先谢谢了
4 个解决方案
#1
其实方法太多了,给你个简单例子吧
Imports System.IO
Imports System.Data.SqlClient
Module Module1
Sub Main()
'建立文件夹
Dim pathRoot As String = "c:\0002\program"
Directory.CreateDirectory(pathRoot)
'在指定的路径上建立文件
Dim pathFile As String = "c:\0002\program\tc.cpp"
File.Create(pathFile)
Dim con As New SqlConnection()
'连接数据库
con.ConnectionString = "server=.;database=你的数据库名; integrated security=true;"
con.Open()
Dim cmd As SqlCommand = con.CreateCommand()
cmd.CommandText = "select q字段 from Exam"
cmd.CommandType = CommandType.Text
Dim sdr As SqlDataReader = cmd.ExecuteReader()
Dim codeStr As String = ""
'读取q字段的值
While sdr.Read
codeStr += sdr(0).ToString()
End While
sdr.Close()
con.Close()
'把codeStr写入已经创建的文件tc.cpp
File.WriteAllText("c:\0002\program\tc.cpp", codeStr)
End Sub
End Module
Imports System.IO
Imports System.Data.SqlClient
Module Module1
Sub Main()
'建立文件夹
Dim pathRoot As String = "c:\0002\program"
Directory.CreateDirectory(pathRoot)
'在指定的路径上建立文件
Dim pathFile As String = "c:\0002\program\tc.cpp"
File.Create(pathFile)
Dim con As New SqlConnection()
'连接数据库
con.ConnectionString = "server=.;database=你的数据库名; integrated security=true;"
con.Open()
Dim cmd As SqlCommand = con.CreateCommand()
cmd.CommandText = "select q字段 from Exam"
cmd.CommandType = CommandType.Text
Dim sdr As SqlDataReader = cmd.ExecuteReader()
Dim codeStr As String = ""
'读取q字段的值
While sdr.Read
codeStr += sdr(0).ToString()
End While
sdr.Close()
con.Close()
'把codeStr写入已经创建的文件tc.cpp
File.WriteAllText("c:\0002\program\tc.cpp", codeStr)
End Sub
End Module
#2
谢谢指点。问题解决,我是这样写的。
Dim sw As StreamWriter
Dim path As String = "C:\StdHNCRE\ " & PN & " \program"
Dim stuDirectory As DirectoryInfo = Directory.CreateDirectory(path)
Dim filePath As String = path + "\" + "tc.cpp"
sw = File.CreateText(filePath)
sw.WriteLine(Me.DataGridView1.Rows(0).DataBoundItem(3).ToString)
sw.Close()
其中PN是一个整型变量。最后一句必须有
Dim sw As StreamWriter
Dim path As String = "C:\StdHNCRE\ " & PN & " \program"
Dim stuDirectory As DirectoryInfo = Directory.CreateDirectory(path)
Dim filePath As String = path + "\" + "tc.cpp"
sw = File.CreateText(filePath)
sw.WriteLine(Me.DataGridView1.Rows(0).DataBoundItem(3).ToString)
sw.Close()
其中PN是一个整型变量。最后一句必须有
#3
Dim sw As StreamWriter
Dim path As String = "C:\ "
Dim stuDirectory As DirectoryInfo = Directory.CreateDirectory(path)
Dim filePath As String = path + "\ " + "tc.cpp "
sw = File.CreateText(filePath)
sw.WriteLine(Me.DataGridView1.Rows(0).DataBoundItem(3).ToString)
sw.Close()
為什麽我一运行出错呢?
索引超出范围。必须为非负值并小于集合大小。
参数名: index
请指教,谢谢
Dim path As String = "C:\ "
Dim stuDirectory As DirectoryInfo = Directory.CreateDirectory(path)
Dim filePath As String = path + "\ " + "tc.cpp "
sw = File.CreateText(filePath)
sw.WriteLine(Me.DataGridView1.Rows(0).DataBoundItem(3).ToString)
sw.Close()
為什麽我一运行出错呢?
索引超出范围。必须为非负值并小于集合大小。
参数名: index
请指教,谢谢
#4
哦,可明白了
#1
其实方法太多了,给你个简单例子吧
Imports System.IO
Imports System.Data.SqlClient
Module Module1
Sub Main()
'建立文件夹
Dim pathRoot As String = "c:\0002\program"
Directory.CreateDirectory(pathRoot)
'在指定的路径上建立文件
Dim pathFile As String = "c:\0002\program\tc.cpp"
File.Create(pathFile)
Dim con As New SqlConnection()
'连接数据库
con.ConnectionString = "server=.;database=你的数据库名; integrated security=true;"
con.Open()
Dim cmd As SqlCommand = con.CreateCommand()
cmd.CommandText = "select q字段 from Exam"
cmd.CommandType = CommandType.Text
Dim sdr As SqlDataReader = cmd.ExecuteReader()
Dim codeStr As String = ""
'读取q字段的值
While sdr.Read
codeStr += sdr(0).ToString()
End While
sdr.Close()
con.Close()
'把codeStr写入已经创建的文件tc.cpp
File.WriteAllText("c:\0002\program\tc.cpp", codeStr)
End Sub
End Module
Imports System.IO
Imports System.Data.SqlClient
Module Module1
Sub Main()
'建立文件夹
Dim pathRoot As String = "c:\0002\program"
Directory.CreateDirectory(pathRoot)
'在指定的路径上建立文件
Dim pathFile As String = "c:\0002\program\tc.cpp"
File.Create(pathFile)
Dim con As New SqlConnection()
'连接数据库
con.ConnectionString = "server=.;database=你的数据库名; integrated security=true;"
con.Open()
Dim cmd As SqlCommand = con.CreateCommand()
cmd.CommandText = "select q字段 from Exam"
cmd.CommandType = CommandType.Text
Dim sdr As SqlDataReader = cmd.ExecuteReader()
Dim codeStr As String = ""
'读取q字段的值
While sdr.Read
codeStr += sdr(0).ToString()
End While
sdr.Close()
con.Close()
'把codeStr写入已经创建的文件tc.cpp
File.WriteAllText("c:\0002\program\tc.cpp", codeStr)
End Sub
End Module
#2
谢谢指点。问题解决,我是这样写的。
Dim sw As StreamWriter
Dim path As String = "C:\StdHNCRE\ " & PN & " \program"
Dim stuDirectory As DirectoryInfo = Directory.CreateDirectory(path)
Dim filePath As String = path + "\" + "tc.cpp"
sw = File.CreateText(filePath)
sw.WriteLine(Me.DataGridView1.Rows(0).DataBoundItem(3).ToString)
sw.Close()
其中PN是一个整型变量。最后一句必须有
Dim sw As StreamWriter
Dim path As String = "C:\StdHNCRE\ " & PN & " \program"
Dim stuDirectory As DirectoryInfo = Directory.CreateDirectory(path)
Dim filePath As String = path + "\" + "tc.cpp"
sw = File.CreateText(filePath)
sw.WriteLine(Me.DataGridView1.Rows(0).DataBoundItem(3).ToString)
sw.Close()
其中PN是一个整型变量。最后一句必须有
#3
Dim sw As StreamWriter
Dim path As String = "C:\ "
Dim stuDirectory As DirectoryInfo = Directory.CreateDirectory(path)
Dim filePath As String = path + "\ " + "tc.cpp "
sw = File.CreateText(filePath)
sw.WriteLine(Me.DataGridView1.Rows(0).DataBoundItem(3).ToString)
sw.Close()
為什麽我一运行出错呢?
索引超出范围。必须为非负值并小于集合大小。
参数名: index
请指教,谢谢
Dim path As String = "C:\ "
Dim stuDirectory As DirectoryInfo = Directory.CreateDirectory(path)
Dim filePath As String = path + "\ " + "tc.cpp "
sw = File.CreateText(filePath)
sw.WriteLine(Me.DataGridView1.Rows(0).DataBoundItem(3).ToString)
sw.Close()
為什麽我一运行出错呢?
索引超出范围。必须为非负值并小于集合大小。
参数名: index
请指教,谢谢
#4
哦,可明白了