Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Database Operation of powershell

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

# region configuration function c function c {# = create object $o=new-object psobject #-database connection configuration-$o | Add-Member-MemberType NoteProperty-Name t-Value 'mysql' # database type $o | Add-Member-MemberType NoteProperty-Name h-Value' localhost' # host address $o | Add-Member-MemberType NoteProperty-Name u-Value 'root' # login $o | Add-Member-MemberType NoteProperty-Name p-Value 'root' # login password $o | Add-Member-MemberType NoteProperty-Name d-Value' monitor' # database accessed by default $o | Add-Member-MemberType NoteProperty-Name c-Value 'utf8' # Encoding #-expand- #-- -expand- # = validate the incoming parameters and output the return value switch ($args [0]) {'t'{return $o.t Break}'h' {return $o.h.boldbreak}'u' {return $o.u.u.Breaking}'p' {return $o.pbornBreaking}'d' {return $o.dittBreaking}'c' {return $o.c Break} default {Write-Warning "the current parameter $args is invalid, please check whether the parameter is correct The available parameters and expressions are as follows: "write-host t: indicates the database type-ForegroundColor Yellow write-host h: indicates the host address-ForegroundColor Yellow write-host u: indicates the account to log in to the database-ForegroundColor Yellow write-host p: indicates the password to log in to the database-ForegroundColor Yellow write-host d: Indicates the default accessed database name-ForegroundColor Yellow write-host d: indicates the character set used to connect to the database-ForegroundColor Yellow exit}} # endregion # region database operation function _ odb_ function _ odb_ {param ($funName # name of the function executed $sql # sql script executed) # from the configuration function c Get configuration information $dbtpye=c t $server=c h $uid=c u $pwd=c p $DBName=c d $char=c c # = = validate the database type Create the database object switch ($dbtpye) {'mssql' {$conn = New-Object System.Data.SqlClient.SqlConnection $cmd = new-object System.Data.SqlClient.SqlCommand $SqlAdapter = New-Object system.Data.SqlClient.SqlDataAdapter break}' mysql' {try {[void] [System.Reflection.Assembly]:: LoadWithPartialName ("mysql.data")} catch {Write-Warning 'Please install the mysql connection component mysql-connector-net-6.5.4'} $conn = New-Object MySql.Data.MySqlClient.MySqlConnection $cmd = new-object mysql.data.mysqlclient.mysqlcommand $SqlAdapter = New-Object mysql.Data.mySqlClient.mySqlDataAdapter break} } try {$conn.ConnectionString= "server=$server Uid=$uid;pwd=$PWD;database=$DBName;charset=$char "$conn.open ()} catch {Write-warning 'database connection error!' Return $false} $cmd.Connection=$conn # region for write, update and other operations with no return value _ exec_ function _ exec_ {$cmd.CommandText=$sql [void] $cmd.ExecuteNonQuery ()} # endregion # region for query, Return operation _ get_ function _ get_ {$cmd.commandtext=$sql $SqlAdapter.SelectCommand = $cmd $DataSet = New-Object System.Data.DataSet [void] $SqlAdapter.Fill ($DataSet "data") return $dataset.tables ['data']} # endregion # to determine the input value And execute the corresponding function switch ($funName) {'_ exec_' {_ exec_ Break}'_ get_' {_ get_;break}} $conn.Close ()} # endregion call _ odb_ _ get_ sql has a return value or _ odb_ _ exec_ sql has no return value

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report