SQL Server Collationの罠

SQLサーバとEntityFrameworkでプログラムを組んでいてちょっとはまった。

 

あまり深く考えずにデータベースを作るときに

ALTER DATABASE XYZ COLLATE Japanese_Unicode_CI_AS_KS

としていた。

 

で、名称マスタの編集画面を作っていて、保存するときに、名称マスタなので、名称が重複するといかんなぁと思って、重複チェックするようにした。

そしたら、なんと、カタカナとひらがなが一緒くたになってしまった!!

おいおい、KSはカタカナとひらがなは別ちゃうんかい!!!

仕方なく、データベースを作るときのCollationを

ALTER DATABASE XYZ COLLATE Japanese_CI_AS_KS

に変えた。

 

なんじゃいな。

 

続きを読む

PostgreSQL initdb fails

勉強用にPostgreSQL 9をダウンロードしてきた。

勉強用なので、インストーラが入ってないZipファイルの方。

 

毎度のことながら、とりあえずデスクトップのフォルダを作ってそこに解凍。

 

まずは、データベースを作るところから。

コマンドプロンプトから

mkdir database

bin\initdb.exe -D database --encoding=UTF8 --no-locale

 

テケテケ・・・と動いてると思ったら、なぜかエラーで終了。

エラーメッセージは

システムオブジェクトの定義をロードしています FATAL : invalid byte sequence for encoding "UTF8"

 

なんじゃこりゃ。

いろいろググって見たところいろんな要因があるみたい。

環境変数 client_encodingをSJISに。 ->変わらず。

chcpでコンソールのコードページを変えてみる。 ->いろいろなコードページにしたけど、やっぱり変わらず。

 

コンソール画面をよ~~~~~く見てたら、あれ?そういえば、デスクトップって日本語・・・

ためしに、丸ごとCの直下のフォルダに移動して再実行。

 

結果:OK!

 

"システムオブジェクトの定義をロードしています ... ok"

 

と表示され、最後には

 

"成功しました。以下を使用してデータベースサーバを起動することができます。"

 

と表示されました。

 

おつかれさま。

 

じゃ、これからnpgsqlとnhibernateのお勉強開始!

 

頑張っていこう。

 

SQL Server 2008 Create/Insert Script

SQL Server 2008 Expressで作ったデータベースとVisualC#2008Expressで作ったアプリケーション。

 

アプリはアプリでいいのだけど、データベースはどうしましょう。

 

ということでいろいろ調べてみたら・・・・Database Publishing Wizardがあるよとお友達が教えてくれた。

でも、SQL2005なんだよねぇ。ダウンロードしたらNativeClientがないって怒られた。

 

改めていろいろ探してみたら、なんとびっくりVS2008に統合されているらしいじゃないですか。

 

もっかいVCS2008起動していろいろやってみたけど・・・・あれ~~?

どうなってるのかな?確かにVCS2008のExpressはデータベース接続はCompactエディションとDBファイルのみ。

そのせいなのか・・・・・。

 

試しにVisual Web Developer 2008 Expressを起動してみると、こっちはなんとちゃんとSQLサーバーに接続できる!

接続したデータベースを右クリックしてPublish to prividerでスクリプトが全部出来ました。

ちゃんとINSERT文も入ってます!

 

よし!

SQL Server sa password reset!!!

Recently I have changed the network from Domain to Workgroup.

Then I removed the domain controller.

 

Today I just realized that I could not create a database on my PC's SQL Server 2008 Express.

It said that I have no permission to do it. Hey, I have administrative permissions!!

 

Then I figured out that the local users were not registered on the SQL Server 2008. Besides I did not want to think of complex password and used "Windows Authentication Mode".

 

Here's what I did.

1.Change the SQL server's authentication mode to "Mixed"

ref: http://blogs.wankuma.com/route/archive/2008/06/12/143047.aspx (Japanese site)

  1a. Open Registry Editor and open "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQLServer"

  1b. Change the value of "LoginMode" to "2" which means Mixed Mode.

 

2.Restart the SQL Server in "Single User Mode"

ref: http://blog.sqlauthority.com/2009/02/10/sql-server-start-sql-server-instance-in-single-user-mode/

  2a.Open Server Contoller applet and modify the startup parameter of SQL Server Service.

  2b.add '-m;' before the other parameters.

  2c.Then restart the SQL Server Service.

 

Now you can log in to the SQL server with your windows account and have all permission you need!!

 

So I first added myself as localuser to SQL Server's user.

Then I changed the password of 'sa'.  Ah, don't forget to enabled the user as well.

 

Thank you Pinal Dave and WANKUMA! You saved my day!!!