Hi guys,
I'm trying to get to grips with ADO, so I've installed SQL server 2008 and made a database and placed a table called Computers in it.
Now ive got the following class...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using System.Data.SqlClient;
namespace WindowsWMIQueryDLL
{
class ActiveDataObjects
{
String SQLServerConnection = "Provider=SQLOLEDB.1;Data Source=local;Initial Catalog=ComputerManagement;Integrated Security=SSPI;";
private OleDbDataAdapter dataAdapter;
private DataSet dataSet;
private DataTable dataTable;
private OleDbConnection conn;
// Database objects
private static string Id;
private static string Name;
//private static string Phonenum;
private System.ComponentModel.Container components = null;
public ActiveDataObjects()
{
string commandstring = "select * from Computer";
// The link between the sql command and the database connection
dataAdapter = new OleDbDataAdapter(commandstring, SQLServerConnection);
// SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=ComputerManagement;Integrated Security=SSPI");
BuildCommands();
dataSet = new DataSet();
dataSet.CaseSensitive = true;
dataAdapter.Fill(dataSet, "Computer");
}
private void BuildCommands()
{
// Use the select command's connection again
OleDbConnection connection =
(OleDbConnection)dataAdapter.SelectCommand.Connection;
// Declare a reusable insert command with parameters
dataAdapter.InsertCommand = connection.CreateCommand();
dataAdapter.InsertCommand.CommandText =
"insert into Computer " +
"(Name) " +
"values " +
"(?)";
dataAdapter.InsertCommand.Parameters.Add("Name", OleDbType.Char, 0, "Name");
}
public void insert(String name)
{
// create a new row, populate it
DataRow newRow = dataTable.NewRow();
// Note: must Trim strings of trailing spaces for Oracle
newRow["Name"] = name.Trim();
// update the database
try
{
dataSet.Tables["Computer"].Rows.Add(newRow);
dataAdapter.Update(dataSet,"Computer");
dataSet.AcceptChanges();
}
catch (OleDbException ex)
{
dataSet.RejectChanges();
}
}
}
}
but whenever i try to connect to my database i get an exception saying...
[DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied.
But the server does exist! I've been playing with it and im currenty logged in using the browers. :S