How to connect mysql in 000webhost? - c#

I create a free host in 000webhost. It is active, here is link http://rndsctvlab.hostei.com/.
Then I create database have information
$mysql_host = "mysql10.000webhost.com";
$mysql_database = "a9127803_data1";
$mysql_user = "<username>";
$mysql_password = "<password>";
Now I want to connect and add data to mysql on this host from my pc. I write a code in C# but it fail,
It warning
Here is my code
string myConnection = "Server=31.170.160.97;Database=a9127803_data1; Port=3306;User ID=<username>;Password=<password>";
conDatabase = new MySqlConnection(myConnection);
string Query = "INSERT into ex1 (ID,Name,Address) values ('" + txt_ID.Text + "','" + txt_name.Text + "','" + txt_address.Text + "');";
MySqlCommand cmdDataBase = new MySqlCommand(Query, conDatabase);
MySqlDataReader myReader;
try
{
conDatabase.Open();
myReader = cmdDataBase.ExecuteReader();
MessageBox.Show("SAVE");
while (myReader.Read())
{
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
How can I fix it? Thank you for help.

I've had this problem before and found that I needed to allow user permissions from IP addresses on MySQL 5 databases.
So for example, if you use MySQL Workbench or MySQL Query Broswer, the query would be like this to allow all hosts:
GRANT ALL ON a9127803_data1.* TO 'a9127803_data2'#'YourIpAddress';
Not quite sure how 000webhost works, or if you can do queries from there, but the above MySQL query is what helped me. You can customise it for each database and user.
EDIT:
Should you need to customise the permissions, look at this MySQL link for more examples using the GRANT syntax
EDIT 2:
If you need to grant permissions via command line, here is an example:
cd C:\mysql //push to your MySQL directory on your server
.\bin\mysql -u user -p
//Enter Password now
grant all on YourDb.YourDbTable to YourUser#YourIpAddress;
Once you enter this, it should grant the permissions for your user on the IP address of your choosing, for the database and table selected.

Related

Access Database - Operation must use an updateable query. (Works fine on local)

I checked other questions when i typed the title in, i can't understand anymore. i need to cool down a bit :)
I started asp.net 1,5 week ago everything was working fine on local and i uploaded my site files to ftp server and i'm getting this error:
Operation must use an updateable query.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.OleDb.OleDbException: Operation must use an updateable query.
Source Error:
//* Goruntulenme sayısını güncelleme //translation updating view count *
// This was the old code after checking other same titled questions i changed to this one and still no luck
//sorgu.Append(" UPDATE sorular SET [goruntulenme] = [goruntulenme] + 1 ");
//sorgu.Append(" WHERE soruid = #soruid ");
//komut = new OleDbCommand(sorgu.ToString(), baglanti);
//komut.Parameters.Add("#soruid", OleDbType.VarChar).Value = sorgusoruid.ToString();
//komut.ExecuteNonQuery();
string SqlString = "UPDATE sorular SET [goruntulenme] = [goruntulenme] + 1 WHERE soruid = ?";
using (OleDbCommand cmd = new OleDbCommand(SqlString, baglanti))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("soruid", sorgusoruid.ToString());
cmd.ExecuteNonQuery();
}
}
Error points to that cmd.ExecuteNonQuery(); line. The things i checked:
*Table has a primary key
*Code works on local just like i need it
*This is a forum part of my project the simplest way i have 3 tables "users" "questions" and "answers"
*i can log in and log out on server. it works fine but can't update. i guess i'm suspecting permissions because this logging in code works (if you want i can translate variable parts of code).
OleDbConnection baglan = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("App_Data\\db.accdb"));
baglan.Open();
string k_ad = Kullanici.Text;
string k_sifre = sifre.Text;
OleDbCommand kontrol = new OleDbCommand("SELECT * FROM Kullanicilar WHERE K_Ad='" + k_ad + "' and K_Sfr='" + k_sifre + "'", baglan);
OleDbDataReader oku = kontrol.ExecuteReader();
if (oku.Read())
{
Session.Add("Kullanici", k_ad);
Response.Redirect("Forum.aspx");
lbl.Visible = false;
}
else
{
lbl.Text = "Kullanıcı adı veya şifre yanlış";
lbl.Visible = true;
}
baglan.Close();
baglan.Dispose();
}
I don't know what else i can give here as info. pls let me know if any more info you need about that and how can i find that information :))
Ohh and i'm using ms-access database, using sql database server seems more complicated to me.
Problem solved, i changed database folder permissions on my hosting service provider's website i logged in a panel with same id pw with the ones i use at filezilla there was settings for folders and they were read only i changed them to full access minus deleting and changing ownership

Access denied for user 'root'#'%' (using password: YES) but logged in as root

I'm aware there are similar questions all over the web, but I can't find anything for this particular issue. I have C# experience, but am pretty new to MySQL, so perhaps there's something I'm not understanding. I'm trying to make a simple select in C# from a MySQL table:
string server = "192.168.2.6";
string database = "productintegration";
string uid = "root";
string password = "Password1";
string connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
MySqlConnection connection = new MySqlConnection(connectionString);
string query = "select * from tcdidataimport";
connection.Open();
MySqlCommand cmd = new MySqlCommand(query, connection);
MySqlDataReader dataReader = cmd.ExecuteReader();
DataTable dt = new DataTable();
if (dataReader.HasRows)
{
dt.Load(dataReader);
}
connection.Close();
And I get the following exception:
Authentication to host '192.168.2.6' for user 'root' using method 'mysql_native_password' failed with message: Access denied for user 'root'#'JUSTINSPERSONAL' (using password: YES)
Seems simple enough... Except that I'm already connecting to that server (via MySQL Workbench) using that exact login.
Here is show grants;
Which seems to me that I should be able to log in using root at whatever I want? When I tried to create another user (CDISelector#'%') and grant privileges I got a similar error:
But I'm logged in as root? Am I missing something here? Finally, here's the results of select user(), current_user();
JUSTINSPERSONAL is my PC, 192.168.2.6 is the MySQL machine's IP. Not sure what I'm missing here but this all seems a little strange.
And it turns out I had the password incorrect. Ha.

How to restore SQL Server backups using asp.net and C#?

How to restore SQL Server backup using C#?
try
{
string test = "D:\\backupdb\\05012017_130700.Bak";
sqlcmd = new SqlCommand("Restore database EmpolyeeTable from disk='D:\\backupdb\\05012017_130700.Bak'", con);
sqlcmd.ExecuteNonQuery();
Response.Write("restore database successfully");
}
catch (Exception ex)
{
Response.Write("Error During backup database!");
}
Quite weird requerement you have right there. I´ve never heard of someone restoring a database backup from a webpage, and as #Alex K. told, it would be quite rare that the user that uses your web application have the required previleges.
Anyway, supposing that everything told above is OK, the code to restore a backup would be this:
Use this:
using System.Data;
using System.Data.SqlClient;
Code:
private void TakeBackup()
{
var conn = new SqlConnection("Data Source=" + Server + ";Initial Catalog=" + Database + ";User Id=" + Username + ";Password=" + Password + ";");
try
{
conn.Open();
SqlCommand command = conn.CreateCommand();
command.CommandText = "RESTORE DATABASE AdventureWorks FROM DISK = 'C:\AdventureWorks.BAK' WITH REPLACE GO";
command.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
conn.Dispose();
conn.Close();
}
}
This is going to work specifically for the problem you posted. Be sure to set all the parameters of your database server on the connection string, it seems from the comments on your question that you are having communication issues. You have to solve that problems before you do anything. Some tips for that:
Be sure you set all the parameters on connection string the right way
Try to connect using another tool like ODBC so you can test all parameters
Check out SQL Network settings to see if TCP/IP is enabled

c# using ActiveDir to get names under a managers org chart?

i am using ODBC to pull data from active directory to get the email for a particular username using the code below.
how can i use AD to pull all the usernames of peolel who report into a prticular manager?
i can transverse the org chart in outlook so im thinking i can do the same using AD...
ideas?
System.Data.OleDb.OleDbConnection con;
System.Data.OleDb.OleDbCommand cmd;
con = new System.Data.OleDb.OleDbConnection("Provider=ADsDSOObject;dsn=Active Directory Provider");
con.Open();
//Create a command object on this connection
string strSQL = "SELECT mail FROM 'LDAP://DC=amrs,DC=win,DC=ml,dc=COM' WHERE samaccountname = '" + UserName.Replace(#"AMRS\", "") + "'";
cmd = new System.Data.OleDb.OleDbCommand(strSQL, con);
try
{
return Convert.ToString ( cmd.ExecuteScalar() );
}
catch (System.Data.OleDb.OleDbException exc)
{
return "ERROR: " + exc.ToString();
}
finally
{
con.Close();
}
See if the manager attribute in AD is set? It should return you the distinguished name of the manager. You can then parse the string to figure out the samAccountName of the manager.
Then just repeat your search using the manager's distinguished name.
Now if the manager attribute isn't set....
Maybe search by department code, and then check the title of everyone in the department?
Might want to look into the Directory Services class.
This link gives you a basic tutorial on how to query AD

How to connect to MySQL database through c#?

I read that I have to download this http://dev.mysql.com/downloads/mirror.php?id=13427#mirrors, but it says that I can't install it because I need .NET Framework. I already have 4.0?!
Use this link, it will work if you have VS.NET 2010 http://dev.mysql.com/downloads/connector/net/
Installer checking for .Net 3.5 or 2.0 mabey =
You should use MySQL Connector/Net 6.3.5 available at mentioned location (http://dev.mysql.com/downloads/connector/net/)
I pieced this together by copy/paste from an existing project then sanitizing it...so it's not been compiled and tested, but you get the idea. So here's some sample code to get you started:
using MySql.Data.Types;
using MySql.Data.MySqlClient;
private void Function()
{
//Set up connection, SqlHost/etc are classwide and declared elsewhere:
MySql connection = new MySqlConnection("SERVER=" + SqlHost + ";DATABASE=" + DatabaseName + ";UID=" + user + ";PASSWORD=" + password + ";pooling=false");
//Setup query:
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
command.CommandText = "your query here";
//Connect to relation system and execute query:
connection.Open();
Reader = command.ExecuteReader();
while(Reader.Read())
{
MessageBox.Show("here's a row from the query response: " + Reader[0].ToString());
}
//Clean up:
connection.Close();
Reader.Close();
}

Categories

Resources