开发者

visual basic .NET connection to sql server compact 3.5

I'm a beginner user of Microsoft Visual Studio 2008 and I want to ask what would be the initial code to connect a Visual Basic 2008 form to a SQL Server compact 3.5. I want to create an add 开发者_如何学JAVAnew account program using the above applications.


Check out connectionstrings.com for the connection string.

Some of my sample code is included below. Extraneous stuff and comments removed for clarity. It's in C# but there are plenty of tools to convert it to VB. Or you can learn another lession by manually converting it yourself ;-)

The GetAllEmployees will return a list of Employees. The list can then be processed / bound as necessary to your controls.

 public static SqlConnection GetConnection()
        {
            //TODO: Use connectionString from app.config file
            string connString = "Data Source=.\\SQLEXPRESS2008;Initial Catalog=Northwind;Integrated Security=True";

            SqlConnection conn = new SqlConnection(connString);
            return conn;
        }


public static List<Employee> GetAllEmployees()
{
    SqlConnection conn = DA.GetConnection();

    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;
    cmd.CommandText = "SELECT * FROM Employees";

    return CreateEmployeeList(cmd);
}




private static List<Employee> CreateEmployeeList(SqlCommand cmd)
{
    List<Employee> employees = new List<Employee>();

    using (cmd)
    {
        cmd.Connection.Open();

        SqlDataReader sqlreader = cmd.ExecuteReader();

        while (sqlreader.Read())
        {
            Employee employee = new Employee
                {
                    FirstName = sqlreader["FirstName"].ToString(),
                    LastName = sqlreader["LastName"].ToString(),
                    StreetAddress1 = sqlreader["Address"].ToString(),
                    City = sqlreader["City"].ToString(),
                    Region = sqlreader["Region"].ToString(),
                    PostalCode = sqlreader["PostalCode"].ToString()
                };

            employees.Add(employee);
        }
        cmd.Connection.Close();
    }
    return employees;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜