How can I connect to a database using Project Maker?

nitin2003

Client
Регистрация
18.09.2012
Сообщения
195
Благодарностей
11
Баллы
18
Hi,

I want to connect to a database and read/write some data to the tables. How can I connect and read/write data from a database if I am creating a template in Project maker and not code creator?

Thanks
 

bigcajones

Client
Регистрация
09.02.2011
Сообщения
1 216
Благодарностей
683
Баллы
113
Use C# code for it.

Download the .Net Connector and install. You will need to reference the MySql.Data.dll that it installs in the GAC.

Then to connect, throw this into a C# action....

Код:
MySql.Data.MySqlClient.MySqlConnection conn;
MySql.Data.MySqlClient.MySqlCommand cmd;
string myConnectionString;

myConnectionString = "server=localhost;uid=[COLOR="#FF0000"]USERNAME[/COLOR];" +
    "pwd=[COLOR="#FF0000"]PASSWORD[/COLOR];database=[COLOR="#FF0000"]DATABASENAME[/COLOR];";

try
{
    conn = new MySql.Data.MySqlClient.MySqlConnection();
    conn.ConnectionString = myConnectionString;
    conn.Open();
	
	cmd = new MySql.Data.MySqlClient.MySqlCommand();
          cmd.Connection = conn;
          cmd.CommandText = "INSERT INTO Authors(Name) VALUES(@Name)";
          cmd.Prepare();
           
          cmd.Parameters.AddWithValue("@Name", "Trygve Gulbranssen");
          cmd.ExecuteNonQuery();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
    return(ex.Message);
}
Of course this is an example of inputting a record into the table. Your queries will vary depending on what you need.
 

nitin2003

Client
Регистрация
18.09.2012
Сообщения
195
Благодарностей
11
Баллы
18
Thanks, I will try this.
Use C# code for it.

Download the .Net Connector and install. You will need to reference the MySql.Data.dll that it installs in the GAC.

Then to connect, throw this into a C# action....

Код:
MySql.Data.MySqlClient.MySqlConnection conn;
MySql.Data.MySqlClient.MySqlCommand cmd;
string myConnectionString;

myConnectionString = "server=localhost;uid=[COLOR="#FF0000"]USERNAME[/COLOR];" +
    "pwd=[COLOR="#FF0000"]PASSWORD[/COLOR];database=[COLOR="#FF0000"]DATABASENAME[/COLOR];";

try
{
    conn = new MySql.Data.MySqlClient.MySqlConnection();
    conn.ConnectionString = myConnectionString;
    conn.Open();
	
	cmd = new MySql.Data.MySqlClient.MySqlCommand();
          cmd.Connection = conn;
          cmd.CommandText = "INSERT INTO Authors(Name) VALUES(@Name)";
          cmd.Prepare();
           
          cmd.Parameters.AddWithValue("@Name", "Trygve Gulbranssen");
          cmd.ExecuteNonQuery();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
    return(ex.Message);
}
Of course this is an example of inputting a record into the table. Your queries will vary depending on what you need.
 

V Munich

Пользователь
Регистрация
20.02.2014
Сообщения
85
Благодарностей
3
Баллы
8
I didn't work for me. I even removed the tags etc..

Ok I did this:



And now it seems to work:



but nothing changes on my mysql table... Any clue?
 
Последнее редактирование:

V Munich

Пользователь
Регистрация
20.02.2014
Сообщения
85
Благодарностей
3
Баллы
8
Never mind, it started working o_O
 

bigcajones

Client
Регистрация
09.02.2011
Сообщения
1 216
Благодарностей
683
Баллы
113
Did you remember to turn on your Wampserver? lol. Done it plenty of times.
 

V Munich

Пользователь
Регистрация
20.02.2014
Сообщения
85
Благодарностей
3
Баллы
8

veeco

Client
Регистрация
27.05.2011
Сообщения
112
Благодарностей
1
Баллы
18
Will it support PHP as well ?
 

V Munich

Пользователь
Регистрация
20.02.2014
Сообщения
85
Благодарностей
3
Баллы
8
Ok I have this:


On ProjectMaker, when I run my project manually step by step, this works:



BUT, when I let my project run to breakpoint (run everything automatically), it doesn't work.. Zenno says that the Own C# code was executed successfully, but nothing changes on my mysql table.

So, in summary:

Play step by step manually = works, mysql table is changed.
Let Zenno play step by step automatically until breakpoint = doesn't work, mysql table is not changed.

Help?????????
 

aleksa77

Client
Регистрация
30.09.2011
Сообщения
914
Благодарностей
90
Баллы
28
I using simple php form for connecting my local mysql, without C#, above form i can read or put values .
 

V Munich

Пользователь
Регистрация
20.02.2014
Сообщения
85
Благодарностей
3
Баллы
8

aleksa77

Client
Регистрация
30.09.2011
Сообщения
914
Благодарностей
90
Баллы
28

bigcajones

Client
Регистрация
09.02.2011
Сообщения
1 216
Благодарностей
683
Баллы
113
Aleksandar is right. When connecting with remote databases it is easier to use a simple php page.
 

aleksa77

Client
Регистрация
30.09.2011
Сообщения
914
Благодарностей
90
Баллы
28
Yes, but for remote acces is important to have alow permision of server admin . If someone have some service and must put information to mysql of client, he can send client one page to put in root and fill with acces data of mysql, ( like wordpress instalation) in this case client and all service not need have permision for remote acces to mysql, becouse zenno go to custom page on client server.
This option have many solution. Example for tube porn poster, i first creating template for each script, this is pain, but with this custom page, you need only names of tables and all job is done.
 
Последнее редактирование:

V Munich

Пользователь
Регистрация
20.02.2014
Сообщения
85
Благодарностей
3
Баллы
8
This is working for me so far:

index.html:
Код:
<html>
<body>

<form action="insert.php" method="post">
Account: <input type="text" name="account">
Status: <input type="text" name="status">
<input type="submit">
</form>

</body>
</html>
insert.php:
Код:
<?php

$host = "1.1.1.1";
$user = "root";
$pass = "password";
$database = "db_0001";
$conn = mysql_connect($host, $user, $pass) or die( mysql_error() );
        mysql_select_db($database) or die( mysql_error() );

$account = mysql_real_escape_string( $_POST["account"] );
$status = mysql_real_escape_string( $_POST["status"] );


$sql = "UPDATE accounts_table SET status = '{$status}' WHERE account = '{$account}'";

$result = mysql_query( $sql ) or die( mysql_error() );
header("Location: index.html");

?>
 

aleksa77

Client
Регистрация
30.09.2011
Сообщения
914
Благодарностей
90
Баллы
28
And simplier:

<?php
$con=mysqli_connect("localhost","root","","wordpress");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

mysqli_query($con,"INSERT INTO wp_posts (post_content)
VALUES ('valuefordb')");



mysqli_close($con);
?>

in html field put " post_content " like variable . You can expand wp_posts (post_content) put more variables:
wp_posts (post_content,post_title)

And in
VALUES ('valuefordb,valuefortitle')
 
  • Спасибо
Реакции: V Munich

lyndaeldo

Новичок
Регистрация
18.03.2015
Сообщения
1
Благодарностей
0
Баллы
1

Кто просматривает тему: (Всего: 2, Пользователи: 0, Гости: 2)