PDA

View Full Version : MaxScript.Net with SQL


david429
03-13-2009, 03:13 PM
Hi gentleman, I have question that confused me for 2 days.
It's about the Maxscript with .Net to do SQL connection
I've read the topic "dotNet + MXS" and I have tried many ways to make it work
but , it's still not working.

I would want to update something from MS SQL
send the SQLcommand like

"UPDATE model_data SET name='newname' WHERE modelid = 25"

This is my script
----------------------------------------------------
dotnet.loadassembly "System"
constring="server=localhost\Server;database=modeldb;Uid=sa;Pwd=sql;"
sqlcon = dotNetObject "System.Data.SqlClient.SqlConnection" constring
sqlcode = "UPDATE model_data SET name='newname' WHERE modelid = 25"
sqlcommand = dotNetObject "System.Data.SqlClient.sqlcommand" sqlcode sqlcon
----------------------------------------------------
I've no idea how to make it work

It's the code in C#.Net I've found
----------------------------------------------------
string constring = "server=localhost\Server;database=modeldb;Uid=sa;Pwd=sql;
using (SqlConnection sqlcon = new SqlConnection(constring))
{
string queryStr = ("UPDATE model_data SET name='newname' WHERE modelid = 25");
SqlCommand sqlcommand = new SqlCommand(queryStr, sqlcon);
sqlcon.Open();
sqlcommand.ExecuteNonQuery();
sqlcon.Close();
}
----------------------------------------------------

Hope somebody can help me ! Thanks

magicm
03-14-2009, 05:30 PM
This is my script
----------------------------------------------------
dotnet.loadassembly "System"
constring="server=localhost\Server;database=modeldb;Uid=sa;Pwd=sql;"
sqlcon = dotNetObject "System.Data.SqlClient.SqlConnection" constring
sqlcode = "UPDATE model_data SET name='newname' WHERE modelid = 25"
sqlcommand = dotNetObject "System.Data.SqlClient.sqlcommand" sqlcode sqlcon
----------------------------------------------------
I've no idea how to make it work
What error are you getting? One thing I noticed is that you used lowercase (the orange part), where it should be "SqlCommand" (.NET is case sensitive).

Martijn

Kameleon
03-15-2009, 12:20 PM
Aren't you forgetting to open the SQL connection and executing the SQL command as in the C# example you have? Cheers.

magicm
03-15-2009, 03:53 PM
Aren't you forgetting to open the SQL connection and executing the SQL command as in the C# example you have? Cheers.
I assumed the OP just left that part out in his post, but yeah without it this script wouldn't do much, other than instancing classes :)

Martijn

david429
03-17-2009, 01:32 PM
I do try this code , but it's still not working

dotnet.loadassembly "System"
constring="server=localhost\Server;database=modeldb;Uid=sa;Pwd=sqlmy;"
con = dotNetObject "System.Data.SqlClient.SqlConnection" constring
con.Open
scmd = dotNetObject "System.Data.SqlClient.SqlCommand"
scmd.Connection = con
scmd.CommandText = "UPDATE avatar_data SET keyword='abc' WHERE (modelid = 38)"
scmd.ExecuteNonQuery



Thank you for your help !

Kameleon
03-17-2009, 02:44 PM
I think you have to use the () in the method calls.

dotnet.loadassembly "System"
constring="server=localhost\Server;database=modeldb;Uid=sa;Pw d=sqlmy;"
con = dotNetObject "System.Data.SqlClient.SqlConnection" constring
con.Open()
scmd = dotNetObject "System.Data.SqlClient.SqlCommand"
scmd.Connection = con
scmd.CommandText = "UPDATE avatar_data SET keyword='abc' WHERE (modelid = 38)"
scmd.ExecuteNonQuery()
con.Close()

thatoneguy
03-17-2009, 06:47 PM
Here's my SQLFunction Library. I just use this for everything. :D


global SQL
struct SQL (
function Connect host:"HOSTPATH" database:"DBNAME" user:"USERNAME" password:"PASSWORD" driver:"DLLPATH"=
(
dotNet.loadAssembly driver
connectionString = ("Database=" + database + ";Data Source=" + host + ";User Id=" + user + ";Password=" + password)
DBConnection = dotNetObject "MySql.Data.MySqlClient.MySqlConnection"
DBConnection.ConnectionString = connectionString
DBConnection.open()
return DBConnection
),
function Query input SQLRequest rush:false=
(
if not rush then
(
cmdObject = input.CreateCommand()
cmdObject.commandText = (SQLRequest as string)
readerObject = cmdObject.ExecuteReader()

SQLDataSet = #()
while readerObject.read() do
(
SQLRow = #()
for j = 0 to (ReaderObject.FieldCount - 1) do
(
append SQLRow ReaderObject.item[j]
)
append SQLDataSet SQLRow
)
readerobject.close()
)
else
(
cmdObject = input.CreateCommand()
cmdObject.commandText = (SQLRequest as string)
SQLDataSet = cmdObject.Executescalar()
)
SQLDataSet
),

function Execute DBConnection SQLRequest =
(
cmdObject = DBConnection.CreateCommand()
cmdObject.commandText = (SQLRequest)
readerObject = cmdObject.ExecuteReader()
readerObject.close()
),

function Insert DBConnection TableName DataArray ID:"NULL" =
(
SQLRequest = ("INSERT INTO " + (TableName as string) + " VALUES ("+(ID as string))
for o in DataArray do
(
append SQLRequest (", '" + (o as string) +"'")
)
append SQLRequest "); SELECT @@IDENTITY AS 'Identity';"
cmdObject = DBConnection.CreateCommand()
cmdObject.commandText = (SQLRequest as string)
readerObject = cmdObject.Executescalar()
return readerObject
),

function Update DBConnection TableName keyname ID DataArray =
(
SQLRequest = ("UPDATE " + (TableName as string) + " SET ")
local i = 1
for o in DataArray do
(
append SQLRequest ((o[1] as string) + " = '" + (o[2] as string) +"'")
if i != DataArray.count do append SQLRequest ", "
i += 1
)
append SQLRequest (" WHERE " + (keyname as string) + " = "+(ID as string)+";")
cmdObject = DBConnection.CreateCommand()
cmdObject.commandText = (SQLRequest as string)
readerObject = cmdObject.Executescalar()
return readerObject
)
)

thatoneguy
03-17-2009, 06:50 PM
Usage would be:

DatabaseObject = SQL.Connect()
SQL.Execute DatabaseObject "UPDATE avatar_data SET keyword='abc' WHERE (modelid = 38)"

david429
03-18-2009, 08:24 AM
Thank you guys , It's work

Thank you very much !

CGTalk Moderation
03-18-2009, 08:24 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.