connecting to a SQL Server database

431 views
Skip to first unread message

Jaya

unread,
Jun 2, 2015, 3:03:46 PM6/2/15
to google-visua...@googlegroups.com
Hi, 

We are using a software tool to build charts and dashboards that connects to various data-sources and displays the charts/dashboards. We would like to start using Google charts and dashboards but I am unable to figure out how to make the connection to the SQL Server database and after the connection is made how do I pull the data and display it as a table to test that the connection has been successfully established. Please help!

Thanks,

Daniel LaLiberte

unread,
Jun 2, 2015, 3:36:06 PM6/2/15
to google-visua...@googlegroups.com
Jaya,

You should look at the documentation for making data queries: https://google-developers.appspot.com/chart/interactive/docs/queries
There are a couple other pages that might help including: https://google-developers.appspot.com/chart/interactive/docs/php_example
Hope that helps get you started.

--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-visualizati...@googlegroups.com.
To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/d/optout.



--
dlaliberte@Google.com   5CC, Cambridge MA
daniel.laliberte@GMail.com 9 Juniper Ridge Road, Acton MA

Jaya

unread,
Jun 2, 2015, 4:37:00 PM6/2/15
to google-visua...@googlegroups.com
Thanks Daniel. I went through the links you had posted. Looks like the examples show how to connect to excel sheets. I would like to use the php example and mimic that to connect to the SQL Server database. But that din't work because the example is using MySQL database and has mysql connection objects. Wasn't sure what would be the sql server connection objects to connect to a SQL Server.

I also tried using asp.net. My dataset is pulling the values from the database but doesn't the page is blank. Below is what I took from one of the forums. Can you please check and let me know what am I doing wrong. 

aspx:

<asp:Literal ID="lt" runat="server"></asp:Literal>             
    <div id="chart_div" style="width: 550px; height: 400px;"></div>

C#:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            BindBarChart();
        }
    }
    private DataTable GetBarData() 
{
    DataTable dt = new DataTable();
    using (SqlConnection conn = new SqlConnection(@"server=servername;uid=username;pwd=password;database=databasename"))
    {
        string cmd = "select UserID, UserFName from Users with(nolock)";
            SqlDataAdapter adp = new SqlDataAdapter(cmd, conn);
            adp.Fill(dt);
            return dt;
    }
}

    private void BindBarChart()
    {
        StringBuilder str = new StringBuilder();
        DataTable dt = new DataTable();

        try
        {
            dt = GetBarData();
            str.Append(@"<script type=text/javascript> google.load( *visualization*, *1*, {packages:[*Table*]});
                   google.setOnLoadCallback(drawChart);

                    function drawChart() {
                    var data = new google.visualization.DataTable();
                    data.addColumn('number', 'UserID');
                    data.addColumn('string', 'UserFName');
                    data.addRows(" + dt.Rows.Count + ");");

            for (int i = 0; i <= dt.Rows.Count - 1; i++)
            {
                str.Append("data.setValue( " + i + "," + 1 + "," + "'" + dt.Rows[i]["UserID"].ToString() + "');");
                str.Append("data.setValue(" + i + "," + 0 + "," + dt.Rows[i]["UserFName"].ToString() + ") ;");
            }

            str.Append("var chart = new google.visualization.Table(document.getElementById('chart_div'));");
            str.Append("chart.draw(data, {title:'Report'");   
            //str.Append("vAxis: { title: 'Client', titleTextStyle: { color: 'red' } }");
            str.Append("}); }");
            str.Append("</script>");
            lt.Text = str.ToString().TrimEnd(',').Replace('*', '"');
        }
        catch (Exception e) { throw e; }
    }

}


Thanks again!

Daniel LaLiberte

unread,
Jun 2, 2015, 5:02:01 PM6/2/15
to google-visua...@googlegroups.com
Jaya,

I can't do much with your aspx and C# code except makes lots of guesses.  It would be better if you could either point to a page, or view the source of the page and post that.  You can email me directly if you don't want to make it public.

--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-visualizati...@googlegroups.com.
To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/d/optout.

Daniel Buttery

unread,
Jun 3, 2015, 6:08:26 AM6/3/15
to google-visua...@googlegroups.com
Hi Jaya.

I do a lot with Google Charts and ASP.Net / MS SQL backends.

The easiest way I've found is to have an ASP.Net Web Service configured to return JSON data. The service connects to your SQL server, and your JS script gets its data from the web service.

Its not ideal, but allows a lot of flexibility.

I'm not able to give you some example code at the moment, but hopefully that might give you some ideas.
Reply all
Reply to author
Forward
0 new messages