1

I want to write an application that talks to a database. The databases are created through phpmyadmin interface. I can talk to these fine through php. What I would like is to populate these databases using a powershell script.

How do I connect to the database ? How do I populate a database ? I can't seem to find any good starting points.

4
  • Which DBMS are you using? Commented Mar 5, 2018 at 20:28
  • @JanezKuhar i take it that means 'DatabaseManagementSystem' ? I've never worked server side before so all this is new to me. All I know is I have wampserver installed on my machine, and I created a database through phpmyadmin Commented Mar 5, 2018 at 20:35
  • 1
    That's what I meant, yeah. I don't know wampserver but it says on their web page that they're using MySQL. You can look into this question. This is in a way a duplicate question. Commented Mar 5, 2018 at 20:40
  • @JanezKuhar that looks spot on, thanks Commented Mar 5, 2018 at 20:42

2 Answers 2

2

Here is a great place to start: https://dbatools.io/ https://dbareports.io/

or you can look here as well: https://www.powershellgallery.com/

Sign up to request clarification or add additional context in comments.

2 Comments

The best SQL people I know recommend dbatools.io for managing SQL databases. I upvoted this even if it is an answer with just a link to other sites.
If the poster had included some code I'd have been happy to add to it.
1

Here's a function to handle this type of task

function Invoke-SQL
{
param (
    [string]$server,
    [string]$database,
    [string]$Query
)

$connectionString = "Data Source=$server; " +
"Integrated Security=SSPI; " +
"Initial Catalog=$database"

$connection = new-object 
system.data.SqlClient.SQLConnection($connectionString)
$command = new-object system.data.sqlclient.sqlcommand($Query, $connection)
$connection.Open()

$adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command

# Use these to populate info #
$dataset = New-Object System.Data.DataSet
$adapter.Fill($dataSet) | Out-Null

$connection.Close()

# displays info #
$dataSet.Tables
}

Here's an example of updating the SQL Database

Invoke-SQL -server 'server' -database 'database' -Query "UPDATE [database].[dbo].[Local] SET Field1 = '$InfoForField1', Field2 = '$InfoForField2'"

You can do whatever you need using this method, as long as you know your SQL queries and how to populate the varaibles with the correct information that you need.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.