需要帮助在 VB.net 中编写循环以根据远程 SQL 数据库中的用户名在 Windows 中创建文件夹
我已经在 Linux 操作系统的 Java 中(或多或少)编写了这个代码,但是我对 .net 的语法不够熟悉,无法将我的 Java 代码转换为 VB2010 代码......所以我希望这里有人可以帮助我有了这个。
问题:
我正在尝试(如标题所示)根据我们的客户数据库中的条目在我的windows盒子上创建文件夹(我们有很多,所以我不打算通过手哈哈)。
我想使用 VB.net 创建一个程序,该程序将通过 SQL 数据库循环查询,获取客户的 customerID,并在特定目录中为每个名称创建文件夹。
我需要翻译的 Java 代码:
//Initializers
Statement stmt;
ResultSet rs;
rs = stmt.executeQuery("SELECT CustomerID FROM customerlist;");
System.out.println("Creating customer user database:\n");
while(rs.next()){
String str1 = rs.getString("CustomerID");
try {
// Create user & Home Directory
Process p = Runtime.getRuntime().exec("sudo useradd " + str1 + " --shell /bin/sh -m " );
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
System.out.println("Created user: \t" + str1);
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
Thread.sleep(100);
} catch (IOException e) {
e.printStackTrace();
}
}
我需要 vb 代码来做同样的事情,但只创建文件夹,并且适用于 Windows 操作系统。
重要说明:
- 我正在使用
Imports MySql.Data.MySqlClient
来建立数据库连接。 - 它是一个远程 mysql 服务器,而不是本地服务器(不知道是否需要此注释)。
- 我们有近 6,000 个客户...(这就是为什么我不手工做!)
- 老板说要从 Java 切换到 VB,所以请不要争论坚持使用 Java 的意义。
看看人们是如何思考的我正在索要整个程序的讲义...这是我到目前为止所拥有的:
Private Sub ButtonLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonLogin.Click
conn = New MySqlConnection()
conn.ConnectionString = "server=xxx" & ";" & "user id=xxx" & ";" & "password=xxx" & ";" & "database=companysqldb"
Try
conn.Open()
MessageBox.Show("Connection Opened Successfully")
'while loop pseudo
'------------------
'set query string to "SELECT CustomerID FROM customerlist;"
'while looping through query
'set stringvar to current loop's CustomerID
'Create directory
If(Not System.IO.Directory.Exists("c:\pub\users\" & stringvar)) Then System.IO.Directory.CreateDirectory("c:\pub\users\" & stringvar)
RichTextBox.AppendText(vbCrLf & "Added folder: " & stringvar)
RichTextBox.ScrollToCaret()
'end while loop
Catch myerror As MySqlException
MessageBox.Show("Error Connecting to Database: " & myerror.Message)
End Try
End Sub
如您所见,我已经写出了大部分内容,但是我的主要困难是 while 循环和SQL 查询。
I already have this coded out (more or less) in Java for Linux OS, however I am not familiar enough with the syntax of .net to translate my Java code into VB2010 code... So I was hoping someone here could help me out with this.
The issue:
I am trying (as the title says) to create folders on my windows box based on the entries in our customer database (we have a lot, so i'm not about to do them by hand lol).
I want to use VB.net to create a program that will loop a query through the SQL database, take the customerIDs of our clients, and create folders for each of those names in a specific directory.
My Java code that I need to translate:
//Initializers
Statement stmt;
ResultSet rs;
rs = stmt.executeQuery("SELECT CustomerID FROM customerlist;");
System.out.println("Creating customer user database:\n");
while(rs.next()){
String str1 = rs.getString("CustomerID");
try {
// Create user & Home Directory
Process p = Runtime.getRuntime().exec("sudo useradd " + str1 + " --shell /bin/sh -m " );
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
System.out.println("Created user: \t" + str1);
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
Thread.sleep(100);
} catch (IOException e) {
e.printStackTrace();
}
}
I need the vb code to do the same thing, but only create the folder, and for the windows OS.
Important notes:
- I am using
Imports MySql.Data.MySqlClient
to make the db connection. - it is a REMOTE mysql server, not a local one (don't know if this note was needed).
- We have close to 6,000 clients... (thus why i'm NOT doing it by hand!)
- The boss says to switch from Java to VB, so no arguing the point of sticking to Java please..
Seeing as how people are thinking that i'm asking for a handout for the entire program... here is what I have so far:
Private Sub ButtonLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonLogin.Click
conn = New MySqlConnection()
conn.ConnectionString = "server=xxx" & ";" & "user id=xxx" & ";" & "password=xxx" & ";" & "database=companysqldb"
Try
conn.Open()
MessageBox.Show("Connection Opened Successfully")
'while loop pseudo
'------------------
'set query string to "SELECT CustomerID FROM customerlist;"
'while looping through query
'set stringvar to current loop's CustomerID
'Create directory
If(Not System.IO.Directory.Exists("c:\pub\users\" & stringvar)) Then System.IO.Directory.CreateDirectory("c:\pub\users\" & stringvar)
RichTextBox.AppendText(vbCrLf & "Added folder: " & stringvar)
RichTextBox.ScrollToCaret()
'end while loop
Catch myerror As MySqlException
MessageBox.Show("Error Connecting to Database: " & myerror.Message)
End Try
End Sub
As you can see, I have most of it already written out, however my primary difficulty is the syntax of the while loop and the SQL Query.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方案:
嗯,经过几个小时的网络搜索,我终于找到了我正在寻找的解决方案。
更改:
至
制作:
SOLUTION:
Well, after several hours of scouring the net, I finally managed to find the solution I was looking for.
changed:
to
Produced: