5

57183N

Usuario (Costa Rica)

Primer post: 3 sept 2011Último post: 3 sept 2011
1
Posts
52
Puntos totales
5
Comentarios
P
Programación: Crear base de datos sql server por código
Hazlo Tu MismoporAnónimo9/3/2011

Aqui les dejo el codigo para crear una base de datos en sql server desde visual .netSe necesitara un formulario como este:Código:Option Explicit OnImports System.Data.OleDbPublic Class Form1Dim conexion As New OleDbConnectionDim comando As New OleDbCommandDim adaptador As New OleDbDataAdapterDim dset As DataSet'insertar datosPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickTryconexion = New OleDbConnection("Provider=SQLOLEDB.1;Integrated Security=SSPI; Persist Security Info=False;User ID=usuario;Initial Catalog=base_ejemplo;Data Source=(local)SQLEXPRESS" )conexion.Open()comando = New OleDbCommand("INSERT INTO info(nombre,apellido,apellido2) Values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "')", conexion)comando.ExecuteNonQuery()adaptador = New OleDbDataAdapter("SELECT * From info", conexion)dset = New DataSetadaptador.Fill(dset, "info" )DataGridView1.DataSource = dset.Tables("info" )DataGridView1.Refresh()Catch ex As ExceptionMsgBox(ex.Message.ToString)FinallyIf conexion.State = ConnectionState.Open Thenconexion.Close()End IfEnd TryEnd Sub'crear base de datosPrivate Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.ClickTryconexion = New OleDbConnection("Provider=SQLOLEDB.1;Integrated Security=SSPI; Persist Security Info=False;User ID=usuario;Initial Catalog=;Data Source=(local)SQLEXPRESS" )conexion.Open()comando = New OleDbCommand("IF EXISTS (SELECT * FROM master..sysdatabases WHERE Name = 'base_ejemplo')" & vbCrLf & "DROP DATABASE base_ejemplo" & vbCrLf & "CREATE DATABASE base_ejemplo", conexion)comando.ExecuteNonQuery()comando.CommandText = "USE base_ejemplo" & vbCrLf & "CREATE TABLE info (nombre NVarChar(50) NOT NULL,apellido NVarChar(50) NOT NULL,apellido2 NVarChar(50))"comando.ExecuteNonQuery()MsgBox("base de datos creada", MsgBoxStyle.Information)Catch ex As ExceptionMsgBox(ex.Message.ToString)FinallyIf conexion.State = ConnectionState.Open Thenconexion.Close()End IfEnd TryEnd Sub'cargar datosPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadTryconexion = New OleDbConnection("Provider=SQLOLEDB.1;Integrated Security=SSPI; Persist Security Info=False;User ID=usuario;Initial Catalog=base_ejemplo;Data Source=(local)SQLEXPRESS" )conexion.Open()adaptador = New OleDbDataAdapter("SELECT * From info", conexion)dset = New DataSetadaptador.Fill(dset, "info" )DataGridView1.DataSource = dset.Tables("info" )DataGridView1.Refresh()Catch ex As ExceptionFinallyIf conexion.State = ConnectionState.Open Thenconexion.Close()End IfEnd TryEnd SubEnd Classcodigo http://stibenamm.hostzi.com/docs/db.rar

52
3
PosteameloArchivo Histórico de Taringa! (2004-2017). Preservando la inteligencia colectiva de la internet hispanohablante.

CONTACTO

18 de Septiembre 455, Casilla 52

Chillán, Región de Ñuble, Chile

Solo correo postal

© 2026 Posteamelo.com. No afiliado con Taringa! ni sus sucesores.

Contenido preservado con fines históricos y culturales.