Notifications
Clear all

[Closed] MaxScript.Net with SQL

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

8 Replies
1 Reply
(@magicm)
Joined: 11 months ago

Posts: 0

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

Aren’t you forgetting to open the SQL connection and executing the SQL command as in the C# example you have? Cheers.

1 Reply
(@magicm)
Joined: 11 months ago

Posts: 0

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

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 !

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
[B]con.Open()[/B]
scmd = dotNetObject "System.Data.SqlClient.SqlCommand"
scmd.Connection = con
scmd.CommandText = "UPDATE avatar_data SET keyword='abc' WHERE (modelid = 38)"
[B]scmd.ExecuteNonQuery()
con.Close()[/B]

Here’s my SQLFunction Library. I just use this for everything.

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


Usage would be:

DatabaseObject = SQL.Connect()
SQL.Execute DatabaseObject “UPDATE avatar_data SET keyword=‘abc’ WHERE (modelid = 38)”

Thank you guys , It’s work

Thank you very much !