How to create a graph in C# using SQL Server database? - c#

I'm using a SQL Server database and C# form, I want to create a graph in C# form with database data.
I use msdn sample codes but it didn't work; no error and no response!
Thank you for helping me ;)
// The Access database
string fileNameString = "data\\chartdata.mdb";
// Initialize a connection string
string myConnectionString = "Data Source=" + fileNameString;
// Define the database query
string mySelectQuery="SELECT Name, Sales FROM REPS ;";
// Create a database connection object using the connection string
SqlConnection myConnection = new SqlConnection(myConnectionString);
// Create a database command on the connection using query
SqlCommand myCommand = new sqlCommand(mySelectQuery, myConnection);
// Open the connection
myCommand.Connection.Open();
// Create a database reader
SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
chart1.Series["Default"].Points.DataBindXY(myReader, "Name", myReader, "Sales");
// Close the reader and the connection
myReader.Close();
myConnection.Close();

you can install and Use reporting services to create graph report and dashboard.
MSDN link for SSRS

Related

Open database into application c#

I have added a SQL Server .mdf database file to my C# application, but when I try to connect with this code, the program causes a connection error.
CODE:
DataSet data;
string con = "Data Source=dbinterno.mdf;";
string queryString = "Select * FROM Dati";
try
{
using (SqlConnection connection = new SqlConnection(con))
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand command = new SqlCommand(queryString, connection);
command.ExecuteNonQuery();
data = new DataSet();
adapter.Fill(data);
MessageBox.Show(data.ToString());
connection.Close();
}
}
catch
{
MessageBox.Show("\n Problemi di connessione al database");
}
The error is:
ERROR IMAGE
Here are a couple observations:
Your connection string will need to be modified. Try using
string con = "Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;";
using Windows Authentication or this:
string con = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;
Password=myPassword;"; using standard security, Source: connectionstrings.com. This should be managed some other way than in code as well. Desktop applications can be de-compiled, and if the password changes, you would need a rebuild. In a ASP.NET application, Microsoft advises to use a web.config file or in the windows registry using a custom subkey.
You will want to use ExecuteReader() for a SELECT statement as ExecuteNonQuery() will not return a result set. See this answer that describes the differences in the types of SQL Server methods
you don't need connection.Close();, the using statement will handle that.

MySqlConnection in C# not working

My connection to my database is not working. It tries to load for a long time then says the connection failed. All i want to do is access the database and print out a name. I am brand new to this but here is what i have so far:
string connectionstring = "uid=user;server=it.et.byu.edu;port=xxxxx;database=database;password=password;";
MySqlConnection connection = new MySqlConnection(connectionstring);
connection.Open();
MySqlCommand cmd = new MySqlCommand("SELECT username FROM Users WHERE username='george'", connection);
try
{
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr.GetString(0));
}
rdr.Close();
}
connection.Close();
Check your connection string and make sure you can connect/manipulate your MYSQL database if you are using XAMPP or other Mysql Tools using your credentials(uid,password).
For introduction this might help you :
ADO.NET With MySQL and MSDE
Connection strings for MySQL
Best Regards

ConnectionString for SQL Server

I have xampp installed in my computer. I am trying to access data with ADO.Net. The connection string I am using is given below:
string connectionString = "Server = localhost; Database = magento; User Id = magento; Password = abcd;";
SqlConnection con = new SqlConnection(connectionString);
string cmdString = "SELECT date_added,title,description,url FROM adminnotification_inbox";
SqlDataAdapter da = new SqlDataAdapter(cmdString, con);
ds = new DataSet();
da.Fill(ds,"prog");
dt = ds.Tables["prog"];
currRec = -1;
totalRec = dt.Rows.Count;
button3.Enabled = true;
I am able to log in with the above user id and password in phpmyadmin, but cannot access the database with the above connection string. please help. Thanks in advance.
MySQL has its own ADO.NET connector: http://dev.mysql.com/downloads/connector/net/6.6.html#downloads
If you use that, you can create a MySqlConnection: http://dev.mysql.com/doc/refman/5.5/en/connector-net-tutorials-intro.html
The basic SqlConnection is used for Microsoft's own SQL Server products.
9-22-14 - Hope others see this if you don't:
You need a driver in your connect string I believe. "MySQL ODBC 3.51 Driver" is the Window's driver name.
string connectionString ="Driver={MySQL ODBC 3.51 Driver}; SERVER= .... ok put the rest of your connect string here.
Note: this is the string to connect to a MySQL db using MS Access VBA:
Dan

How I create a SQL Server Compact 3.5 .sdf file and connect to it?

I have created a SQL Server Compact Database (.sdf file) and I want to be connected to it for do some insert , delete ... .
This is my creation code for it:
if (File.Exists(dbfilename))
File.Delete(dbfilename);
string connectionString = "Data Source=" + dbfilename + """;
SqlCeEngine engine = new SqlCeEngine(connectionString);
engine.CreateDatabase();
engine.Dispose();
SqlCeConnection conn = null;
try
{
conn = new SqlCeConnection(connectionString);
conn.Open();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "CREATE TABLE Contacts (ID uniqueidentifire, Address ntext)";
cmd.ExecuteNonQuery();
}
catch { }
finally
{
conn.Close();
}
Is it true?
How can I connect to it?
That connection string is broken.
"
is not a valid entity where you are trying to use it. Fix your connection string.
Also, you will need to associate the command with the connection, either in the constructor or after the fact.
Please read through an example such as this one, which was the first google result for "connect sql compact example c#":

Excel contents into SQL Server table using Visual Studio 2008

I'm creating an application where the user clicks on a button, browses for an Excel file, and the data is copied into the data table created in the database.
I am using VS2008 and SQL Server 2005.
I wrote code for opening the file of course, and created a dataTable and its dataColumns in the .cs file. What else should I do?
Thank you.
You could do as Krishna advises... but that code will only work if the columns in the excel file and Database columns are the same in number and order. For ease of maintainability and mapping down the line I highly recommend you use a combination of Linq to Excel and Linq to SQL as in this article:
http://solidcoding.blogspot.com/2008/01/linq-to-excel-sql-import.html
You can write like the code below to dump the data into the SQL Server table
string excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("ImportFile.xls") + ";Extended Properties=""Excel 8.0;HDR=Yes;""";
using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
{
OleDbCommand command = new OleDbCommand("Select * FROM [NameOfTheDataSheet$]", connection);
connection.Open();
using (DbDataReader dataReader = command.ExecuteReader())
{
string sqlConnectionString = "SQL Connection String";
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "ExcelDataTable";
bulkCopy.WriteToServer(dataReader);
}
}
}
Let me know if you have different requirements.

Categories

Resources