|
-
Jun 1st, 2006, 02:45 PM
#1
Thread Starter
Hyperactive Member
[2005] cannot save to database
hey, still trying to figure out this database stuff, and now i have a new problem... i connect to the db successfully, then i have the following code, then i close the connection:
VB Code:
Dim a As New SqlCommand("INSERT INTO Table1 VALUES (1, 'hey')", conn)
a.ExecuteReader()
i even check how many records were entered in another button, and it says 1, but when i exit the program, and check again, it's not there... what am i missing?
-
Jun 1st, 2006, 02:50 PM
#2
Re: [2005] cannot save to database
try
a.executenonquery
executereader is when you are doing a select statement and are returning records into a datareader object..
-
Jun 1st, 2006, 02:52 PM
#3
Thread Starter
Hyperactive Member
Re: [2005] cannot save to database
i actually had executenonquery first, and then i changed it to executereader...
-
Jun 1st, 2006, 02:56 PM
#4
Re: [2005] cannot save to database
executenonquery returns an integer value based on the number of records affected.
so what value is it returning when you run it?
-
Jun 1st, 2006, 02:59 PM
#5
Thread Starter
Hyperactive Member
Re: [2005] cannot save to database
-
Jun 1st, 2006, 03:03 PM
#6
Re: [2005] cannot save to database
but you are saying if you go and check the database, the record is not there??
-
Jun 1st, 2006, 03:09 PM
#7
Thread Starter
Hyperactive Member
Re: [2005] cannot save to database
ok i have two buttons - button1 puts in the row (or at least supposed to), button2 shows how many rows are in db. i click button1, then i click button2 and it msgbox's me saying there is 1 row. then i exit app, and start it up again. i click button2 and it msgbox's "0".
btw is there any other way of browsing the db besides right-clicking on Table1 under Data Sources and going to Preview Data?
-
Jun 1st, 2006, 03:12 PM
#8
Re: [2005] cannot save to database
You would need a program like Enterprise Manager (I dont think that come with SQL Express if thats what you are using)
There may be other ways.. but I always use Enterprise Manager (sometimes query analyzer)... these are standard programs that come with SQL Server
what does the code look like in button2??
-
Jun 1st, 2006, 03:14 PM
#9
Thread Starter
Hyperactive Member
Re: [2005] cannot save to database
VB Code:
conn.Open()
Dim m_daStudents As New SqlDataAdapter("SELECT * FROM Table1", conn)
Dim m_DataSet As New DataSet()
m_daStudents.TableMappings.Add("Table", "Table1")
m_daStudents.Fill(m_DataSet)
MsgBox(m_DataSet.Tables("Table1").Rows.Count)
conn.Close()
there's probably an easier way of checking, but i am not there yet... do i even have to put it in dataset first?!
-
Jun 1st, 2006, 03:19 PM
#10
Re: [2005] cannot save to database
if you just wanted a count, then no, you could execute a simple scalar query... which returns just a single value.. something like this...
VB Code:
Dim cmd As New SqlClient.SqlCommand("SELECT COUNT(*) FROM table1", conn)
Dim NumOfRecs As Integer
NumOfRecs = Convert.ToInt32(cmd.ExecuteScalar)
Messagebox.Show(NumOfRecs.ToString)
conn.Close()
-
Jun 1st, 2006, 03:23 PM
#11
Thread Starter
Hyperactive Member
Re: [2005] cannot save to database
cool, but it still displays 0, any ideas?
-
Jun 1st, 2006, 03:36 PM
#12
Re: [2005] cannot save to database
well what happens when you do view the data in the data sources window in the IDE?
are there records in there?
-
Jun 1st, 2006, 03:45 PM
#13
Thread Starter
Hyperactive Member
Re: [2005] cannot save to database
well, it gives me a nice error message: "An attempt to attach an auto-named database for file C:\Very Long Path\MyDatabase.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."
... at this point I am starting to like XML more and more ...
i searched the error message, but didn't find much useful info.
-
Jun 1st, 2006, 04:10 PM
#14
Re: [2005] cannot save to database
what database are you using?
-
Jun 1st, 2006, 04:15 PM
#15
Thread Starter
Hyperactive Member
Re: [2005] cannot save to database
as in the type or the actual file name? if you mean the type then SQL Server
-
Jun 1st, 2006, 04:17 PM
#16
Re: [2005] cannot save to database
yeah I meant type but I also meant version.. sorry Im just trying to collect as much info to help you so i dont run you around in circles
-
Jun 1st, 2006, 04:20 PM
#17
Thread Starter
Hyperactive Member
Re: [2005] cannot save to database
hey i appreciate all the help i can get... i downloaded latest version SQL Express from microsoft's website
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|