开发者

Need help coding a loop in VB.net to create folders in windows based on usernames in remote sql db

开发者 https://www.devze.com 2023-03-23 00:16 出处:网络
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 c

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.


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


 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()
0

精彩评论

暂无评论...
验证码 换一张
取 消