需要帮助在 VB.net 中编写循环以根据远程 SQL 数据库中的用户名在 Windows 中创建文件夹

发布于 2024-11-26 06:27:22 字数 2665 浏览 1 评论 0原文

我已经在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半岛未凉 2024-12-03 06:27:22

解决方案:

嗯,经过几个小时的网络搜索,我终于找到了我正在寻找的解决方案。

更改:

    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

conn.Open()
MessageBox.Show("Connection Opened Successfully")
Dim command As New MySqlCommand("SELECT CustomerID from customerlist", conn)
        Dim stringvar As String
        Dim count As Integer = 1

        myReader = command.ExecuteReader
        While (myReader.Read())
            stringvar = myReader(0).ToString

            If (Not System.IO.Directory.Exists("c:\pub\users\" & stringvar)) Then
                System.IO.Directory.CreateDirectory("c:\pub\users\" & stringvar)
                TextBox.AppendText(vbCrLf & count & ":   Created folder for CustomerID: " & stringvar)
                TextBox.ScrollToCaret()
            Else
                TextBox.AppendText(vbCrLf & count & ":   Could *NOT* creat folder for CustomerID: " & stringvar)
                TextBox.ScrollToCaret()
            End If

            count = count + 1
        End While

制作:

CustomerID: 11112
CustomerID: 11113
CustomerID: 11114

SOLUTION:

Well, after several hours of scouring the net, I finally managed to find the solution I was looking for.

changed:

    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

to

conn.Open()
MessageBox.Show("Connection Opened Successfully")
Dim command As New MySqlCommand("SELECT CustomerID from customerlist", conn)
        Dim stringvar As String
        Dim count As Integer = 1

        myReader = command.ExecuteReader
        While (myReader.Read())
            stringvar = myReader(0).ToString

            If (Not System.IO.Directory.Exists("c:\pub\users\" & stringvar)) Then
                System.IO.Directory.CreateDirectory("c:\pub\users\" & stringvar)
                TextBox.AppendText(vbCrLf & count & ":   Created folder for CustomerID: " & stringvar)
                TextBox.ScrollToCaret()
            Else
                TextBox.AppendText(vbCrLf & count & ":   Could *NOT* creat folder for CustomerID: " & stringvar)
                TextBox.ScrollToCaret()
            End If

            count = count + 1
        End While

Produced:

CustomerID: 11112
CustomerID: 11113
CustomerID: 11114
深海少女心 2024-12-03 06:27:22
 cmd.Connection = conn
        conn.Open()

        Dim cnt As Integer
        Dim CustCode As String


        cmd.CommandText = "Select * from Checks"

        rd = cmd.ExecuteReader

        While rd.Read()
            cnt = cnt + 1
        End While

        If cnt = 0 Then
            cnt = cnt + 1
        Else
            cnt = cnt + 1
        End If
        CustCode = "Cust_00" & cnt
        TextBox1.Text = CustCode
        rd.Close()
 cmd.Connection = conn
        conn.Open()

        Dim cnt As Integer
        Dim CustCode As String


        cmd.CommandText = "Select * from Checks"

        rd = cmd.ExecuteReader

        While rd.Read()
            cnt = cnt + 1
        End While

        If cnt = 0 Then
            cnt = cnt + 1
        Else
            cnt = cnt + 1
        End If
        CustCode = "Cust_00" & cnt
        TextBox1.Text = CustCode
        rd.Close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文