I am so sorry, I am new here and I do not know how to move a post, I posted this in the c++ section by accident, I am so sorry, I need it in C# . Thank you.
------------------------------------------------------------------------------------------------------
Hello there, I am new here, and Thank you
for taking the time to read this and hopefully I can get helped :) ...
I will cross my fingers.
Please CLICK HERE
to see what I have planned. then you will be able to understand the
question. =D ... I want to make it simple to understand, don't want to
make any body think toomuch. I feel bad.
I want to execute the
QUERY below, by clicking on button1, and displaying result on Result
Window. (I havent found out which result window would be more
propper... DataGrid?)
-------------
USE [account];
GO
select * from user_profile where login_flag = 1100
----- OR -----
USE [cash];
UPDATE user_cash
SET amount=0
WHERE amount=294765
-------------
I
want to, for example, click on BUTTON1 and execute the above query, and
then show the results of the query in the RESULT window...
This is what is going on:
I have a SQL 2000 database, and I want that software to be able to:
Connect to my database thru IP, execute ANY queries and display results in RESULT WINDOW.
...
I don't know, If i learn that, I think that would make me understand
everything else easyer. I have no idea what I am doing, all I know how
to do is ,
Click on NEW,PROJECT,C# Form Application. ><'' ... I wasnt able to go farther.
Loading
Roei BarPosted Sep 25, 2009, 5:05 PM
sharpdevelop
http://www.icsharpcode.net/OpenSource/SD/
its an Open Source C# Development UI, which has almost all features like Visual Studio and capble of running Visual Studio Projects
thiago costaPosted Sep 26, 2009, 12:03 PM
I am literally crying...
Please help me, I will send paypal money for any classes if anyone is interested in being my personal teacher =D ... I am poor but I can pay
EDIT ... Problem Solved.
FORM
ing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DatabaseExec;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SQLDataAccess.ServerName = "SERVER IP";
SQLDataAccess.UserName = "dklogin2";
}
private void buttonLogin_Click(object sender, EventArgs e)
{
string password = textBoxPassword.Text;
if (SQLDataAccess.ValidateConnection("Account", password) == true)
{
SQLDataAccess.Password = password;
MessageBox.Show("Login Successful");
textBoxPassword.Text = "";
textBoxPassword.UseSystemPasswordChar = false;
}
else
{
MessageBox.Show("Login failed");
}
}
private void buttonExecute_Click(object sender, EventArgs e)
{
SQLDataAccess dataAccess = new SQLDataAccess();
dataAccess.Select("Account", "SELECT user_no, login_flag from USER_PROFILE WHERE login_flag=1100");
dataGridView1.DataSource = dataAccess.Data;
labelCountRows.Text = dataAccess.Data.Rows.Count.ToString() + " rows returned";
}
private void buttonBanUser_Click(object sender, EventArgs e)
{
DataTable dt = SQLDataAccess.Execute("Character", "SELECT user_no from user_character WHERE character_name='"+textBoxPassword.Text+"'");
if(dt.Rows.Count == 0)
{
MessageBox.Show("Character not found");
return;
}
string user_no = dt.Rows[0]["user_no"].ToString();
SQLDataAccess.ExecuteNoReturn("Account", "Update USER_PROFILE SET login_flag = 1200 WHERE user_no='" + user_no + "'");
//DataTable dt1 = SQLDataAccess.Execute("Account", "Update USER_PROFILE SET login_flag = 1200 WHERE user_no='" + user_no + "'\nSELECT @@ROWCOUNT");
//if (dt1.Rows.Count == 0)
//{
// MessageBox.Show("Account for the Character not found");
// return;
//}
//SQLDataAccess.Execute("Account", String.Format("Update USER_PROFILE SET login_flag = 1200 WHERE user_no='{0}'", user_no));
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
SQLdataacess:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DatabaseExec
{
public class SQLDataAccess
{
public static string ServerName;
public static string UserName;
public static string Password;
public DataTable Data;
private DataSet DsData;
private SqlDataAdapter mDataAdapter;
private SqlCommandBuilder mCmdBuilder;
public void Select(string databaseName, string cmdToExecute)
{
DsData = new DataSet();
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = SQLDataAccess.ServerName;
builder.InitialCatalog = databaseName;
builder.UserID = SQLDataAccess.UserName;
builder.Password = SQLDataAccess.Password;
mDataAdapter = new SqlDataAdapter();
mDataAdapter.SelectCommand = new SqlCommand(cmdToExecute, new SqlConnection(builder.ConnectionString));
mCmdBuilder = new SqlCommandBuilder(mDataAdapter);
mDataAdapter.Fill(DsData);
Data = DsData.Tables[0];
}
public void SaveChanges()
{
mDataAdapter.Update(Data);
}
public static DataTable Execute(string databaseName, string cmdToExecute)
{
DataSet ds = new DataSet();
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = SQLDataAccess.ServerName;
builder.InitialCatalog = databaseName;
builder.UserID = SQLDataAccess.UserName;
builder.Password = SQLDataAccess.Password;
using(SqlDataAdapter da = new SqlDataAdapter(cmdToExecute, new SqlConnection(builder.ConnectionString)))
{
da.Fill(ds);
}
return ds.Tables[0];
}
public static void ExecuteNoReturn(string databaseName, string cmdToExecute)
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = SQLDataAccess.ServerName;
builder.InitialCatalog = databaseName;
builder.UserID = SQLDataAccess.UserName;
builder.Password = SQLDataAccess.Password;
using (SqlConnection conn = new SqlConnection(builder.ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(cmdToExecute, conn))
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
public static bool ValidateConnection(string databaseName, string password)
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = SQLDataAccess.ServerName;
builder.InitialCatalog = databaseName;
builder.UserID = SQLDataAccess.UserName;
builder.Password = password;
using (SqlConnection conn = new SqlConnection(builder.ConnectionString))
{
try
{
conn.Open();
}
catch (SqlException)
{
return false;
}
return true;
}
}
}
}
Thank is how I made it work with a little help of my Indian friend Vinnoth =D
Thank you all SOMUCH . This is so complicated, I will not give up.
thiago costaPosted Sep 25, 2009, 8:49 PM
Hi Thiago,
Its a little more complicated than you want...
You set up a System.Data.Common.DataAdapter with the SELECT query.
You call Fill to get the results of the query into a System.Data.DataTable
Then:
You set up a DataGridView and set it's DataSource property to the above DataTable
Voila...
Please mark this post as an answer if it helps you
Nir[/quote]
So sorry, I am home now, I have the compiler open...
when you said to setup System.Data.Common.DataAdapter , what did you mean?
I WANT TO CRY :'( ...
After researching, i decided that I need to activate a connection before anything works anyways, like... a CONNECT button, then the other buttons can execute the queries .....
This is my program:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = "Persist Security Info=False;Integrated Security=SSPI;database=account;server=IPxx.xxx.57.x;user id=sa;password=flamxxxssdf;Connect Timeout=30";
myConnection.Open();
try
{
myConnection.Open();
}
catch (Exception x)
{
Console.WriteLine(x.ToString());
}
}
}
}
I clicked on BUTTON 1, and I don't know if it is connecting or not ... It did not give me ANY messages at all :( something I did wrong so far?
Nir BarPosted Sep 25, 2009, 11:15 AM
Good luck
Please mark this post as an answer if it helps you
Nir
thiago costaPosted Sep 25, 2009, 10:43 AM
Thank you !!!
Nir BarPosted Sep 25, 2009, 10:18 AM
Its a little more complicated than you want...
You set up a System.Data.Common.DataAdapter with the SELECT query.
You call Fill to get the results of the query into a System.Data.DataTable
Then:
You set up a DataGridView and set it's DataSource property to the above DataTable
Voila...
Please mark this post as an answer if it helps you
Nir